-2
import java.util.InputMismatchException;  import java.util.Scanner;

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean nvalid = true; // boolean to get out of do while when user input number
        int a = 0;
        System.out.println("enter number a");
        do {
            try {
                a = sc.nextInt();
                nvalid = true;
            } catch (InputMismatchException e) {
                System.out.println("please enter number only");
                nvalid = false; //make the boolean false so do while will let user enter number again.
            }
        } while (nvalid == false);
        System.out.println("out of do while");
    }

Hi , i am using netbeans to practice java exception, i want to catch exception when user does not enter number.

The code above causes infinite loop and prevents user from entering value for variable a.

Pbd
  • 1,219
  • 1
  • 15
  • 32

1 Answers1

3

Well you are successfully catching the exception. That's not the problem.

The real problem is caused by what happens before the exception is caught. If nextInt() cannot parse an integer, it puts back all of the characters that it looked at.

So ... when it goes around the loop again, your code is attempting to read exactly the same characters as before. That will fail, and fail, and fail ...

Solution: use nextLine() to skip over the "bad input" before asking the user to try again. You could put this in the exception's catch block.


Note that this is a bad idea / bad habit:

  .... while (nvalid == false);

You should not use == to test if a boolean is true or false. Do this instead:

  .... while (!nvalid);
Graham
  • 7,431
  • 18
  • 59
  • 84
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • A `break` can simplify this a bit further as well. – pvg Apr 23 '17 at 04:11
  • That is true. However, I'm focusing on what I think are the most important things for Nhan to learn from this. – Stephen C Apr 23 '17 at 04:12
  • Shouldn't he primarily learn to do research? He obviously didn't. – Tom Apr 23 '17 at 04:15
  • Oh it intended as a comment rather than a 'change your answer' thing. Although you probably _should_ use italics over ALL CAPS for emphasis :) – pvg Apr 23 '17 at 04:17
  • Hi, thank you for the explain about how nextInt() working, i try nextLn() before but netbeans show error that int variable not compatible with nextLn(). I also try set a = 0 before set nvalid = false to not trigger the catch when the loop re-run, but the result still the same. About the writing habit, i did read your pitfall link, thanks for that too :D – Nhan Nguyen Apr 23 '17 at 04:24
  • You shouldn't be assigning the nextLine result to an int in this case. You should be throwing the bad input away! – Stephen C Apr 23 '17 at 04:27
  • err... could you please get me a sample how to use throw bad input, i try using ctrl + space but see no suggestion, and use throw exception e not working too. – Nhan Nguyen Apr 23 '17 at 04:40
  • I found the solution in the quote link by the mod, using reader.next() will solve this, thank you for take your time and reply to my question. – Nhan Nguyen Apr 23 '17 at 04:51
  • Here is an example: `sc.nextLine();`. Note ... the result of the call is not assigned to anything. – Stephen C Apr 23 '17 at 04:53