Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:25:34+00:00 2026-06-17T17:25:34+00:00

Alright, guys, I’m trying to get a basic hang on Java and I figured

  • 0

Alright, guys, I’m trying to get a basic hang on Java and I figured I would make something a bit more complicated than usual. What I have in this method is an attempt to first check if the first character can be an int, and if it is, I want to see how far that number will go in the input (so if the user inputted 455+60, I’d want to pull out the 455 and stop at the +).

For some reason, if the input is 500t, it will work fine. But if the index ends after the number (say the input is just 500), it has a problem. I am thinking the for loop keeps going for some reason. Can someone tell me where I messed up? I added an if statement to see if I could stop it, but to no avail.

public void init(){
    int chars = 0;
    /*
     * Input fancy intro here.
     */
    clearArray();
    String uInput = getInput();
    rawInput=uInput.toCharArray();
    cleanArray(rawInput);
    try{Integer.parseInt(Character.toString(rawInput[0]));
    boolean isNum = true;
    for(int i=0;isNum==true;i++){
        if(i != rawInput.length){
        try{Integer.parseInt(Character.toString(rawInput[i]));}
        catch (NumberFormatException s){
            isNum = false;
            chars = i;
            System.out.println("The number has: "+chars+" characters.");
        }
        }
        }       
    }
    catch (NumberFormatException s){
        System.out.println("What is this madness?");

    }



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:25:35+00:00Added an answer on June 17, 2026 at 5:25 pm

    Here, you avoid asking for rawInput[rawInput.length]:

    if(i != rawInput.length)
    

    … but you do keep going for the next iteration.

    Is there any reason you can’t just do:

    for (int i = 0; i < rawInput.length && isNum; i++)
    

    ? I would also suggest using Character.isDigit or a custom check rather than converting each character to a string, then parsing it and catching an exception.

    Note that Character.isDigit will return true for non-ASCII digits, which you may not want. You quite possibly just want a simple check of:

    char c = rawInput[i];
    if (c < '0' || c > '9') {
        isNum = false;
    }
    

    Also note that a regular expression may well be a better solution for this…

    Oh, and if you’ve got an empty string, that will count as a number at the moment…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright guys i'm trying to make an Asteroids type game and I need to
Alright, I'm trying to use the basic document.getElementsByTagName function, but each time I do,
Alright guys, i'm trying to use the variable in the getIndividualMap function but i'm
Alright guys, I really hurt my brain over this one and I'm curious if
Alright, here I am again trying to write code from scratch and I can't
Alright, an easy one for you guys. We are using ActiveReport's RichTextBox to display
Alright guys, Need some help! Im working with asp.net mvc3 razor (and am fairly
Alright guys thanks for helping me with the 100% height problem. it works now..
Alright guys, I'm quite confused. So, I have an NSDictionary which is populated by
alright, guys: I met a problem during communicating between two classes, the ViewControllerClass MyViewController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.