0

So I'm writing code here just for fun but I've come up with an error that I just can't seem to fix. This block of code is supposed to take in an int... at first i had the hasNextInt() in the while loop alone to try and ensure i'm getting the correct input, but as fate would have it.. i got the exception. I then added a try catch to it thinking maybe i just did something wrong... and still i get the same error. I don't know whats wrong here. this is actually my 1st time using a try catch block (still kind of a noob). it looks good to me and i've looked at the documentation online and done some minor research but to no avail. can anyone identify whats wrong here? check it out:

do{
    System.out.println("How much AP do you want to allocate towards HP? ");

    try {//added try catch... still throwing the exception..

        while(!in.hasNextInt()){//this should've been enough, apparently not
            System.out.println("That is not a valid input, try again.");
            in.nextInt();
            }
    } catch (InputMismatchException e) {
        System.out.print(e.getMessage()); //trying to find specific reason.
    }
    hpInput = in.nextInt();
}while(hpInput < 0 || hpInput > AP);

if i entered a string it would give me the "That is not a valid input, try again." line.. but the exception would still occur right after instead of just looping until an actual int is detected... help plz..

  • You're asking the question backwards, you're checking to see if the input is valid and then ask the user to input a new value...It might be better to get the input as a `String` and then use another `Scanner` to validate it... – MadProgrammer Feb 26 '15 at 02:57

2 Answers2

2

Your while loop should look something like this

while(!in.hasNextInt()){ // <-- is there an int?
    System.out.println("That is not a valid input, try again.");
    // in.nextInt(); // <-- there is not an int...
    in.next(); // <-- this isn't an int.
}

Because the Scanner doesn't have an int.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You can't really validate the value in the Scanner until something has been input, but once it's input, it's too late to validate it...

Instead, you could use a second Scanner to validate the String result you are getting from the user via the keyboard, for example

Scanner kbd = new Scanner(System.in);
int result = -1;
do {
    System.out.println("How much AP do you want to allocate towards HP? ");
    String value = kbd.nextLine();
    Scanner validate = new Scanner(value);
    if (validate.hasNextInt()) {
        result = validate.nextInt();
    }
} while (result < 0);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • So let me just try to understand this more... the value here = kbd.nextLine() which i thought was only for string inputs? however if an int is entered instead of a string... it just accepts it. Am i missing something? btw it works now, so thank you very much, im just trying to really understand what the code is doing here. – Jeffrey Soto Feb 27 '15 at 06:32
  • Yep, `kdb.nextLine` basically takes everything up the new line marker from the input and returns it as a `String`, then you can perform additional "validation" on using a separate `Scanner`, in this case... – MadProgrammer Feb 27 '15 at 06:41