3

I am trying add catch blocks to my program to handle input mismatch exceptions. I set up my first one to work inside of a do while loop, to give the user the opportunity to correct the issue.

System.out.print("Enter Customer ID: ");
int custID=0;
do {
    try {
        custID = input.nextInt();
    } catch (InputMismatchException e){
        System.out.println("Customer IDs are numbers only");
    }
} while (custID<1);

As it stands, if I try to enter a letter, it goes into an infinite loop of "Customer IDs are numbers only".

How do I make this work properly?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
user1873736
  • 101
  • 2
  • 12

4 Answers4

3

Be aware that When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

To avoid "infinite loop of "Customer IDs are numbers only".", You need to call input.next(); in the catch statement to to make it possible to re-enter number in Console

From

statement

catch (InputMismatchException e) {
            System.out.println("Customer IDs are numbers only");

To

catch (InputMismatchException e) {
            System.out.println("Customer IDs are numbers only");
            input.next();
        }

Example tested:

Enter Customer ID: a
Customer IDs are numbers only
b
Customer IDs are numbers only
c
Customer IDs are numbers only
11
Mengjun
  • 3,159
  • 1
  • 15
  • 21
  • return type of next() method is String, is this the solution ? – Suganthan Madhavan Pillai Nov 19 '13 at 16:49
  • When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method. next() method is calling is just to make it possible to re-enter number in Console. – Mengjun Nov 19 '13 at 16:53
  • then how to assign it, to the integer variable custId. If this the case we try this inside the try scope only why inside catch? – Suganthan Madhavan Pillai Nov 19 '13 at 16:56
  • Do not need to assign it. The method next() is calling is just to make it not be in infinite loop When a scanner throws an InputMismatchException. Then the do-while loop is still executing. And custId is stiil read by code custID = input.nextInt(); – Mengjun Nov 19 '13 at 17:02
  • Yes,with your little clue, i posted my answer working great – Suganthan Madhavan Pillai Nov 19 '13 at 17:15
2

What's happening is that you catch the mismatch, but the number "wrong input" still needs to be cleared and a .next() should be called. Edit: since you also require it to be greater than or equal to 1 per your do/while

boolean valid = false;
while(!valid) {
    try {
        custID = input.nextInt();
        if(custID >= 1) //we won't hit this step if not valid, but then we check to see if positive
            valid = true; //yay, both an int, and a positive one too!
    }
    catch (InputMismatchException e) {
        System.out.println("Customer IDs are numbers only");
        input.next(); //clear the input
    }
}
//code once we have an actual int
Compass
  • 5,867
  • 4
  • 30
  • 42
  • How is this any different from what OP posted from a functionality point of view? The reason why the OP:s code loops infinitely is that nextInt() does not consume the next token if it does not have an integer format. Your code will show exactly the same behaviour. – Alderath Nov 19 '13 at 16:31
  • @Alderath Hmmm, good point. The .next() is needed to clear it. And then it is technically equal. My mistake. – Compass Nov 19 '13 at 16:35
  • return type of next() method is String, is this the solution ? – Suganthan Madhavan Pillai Nov 19 '13 at 16:53
0

Why not use a scanner object to read it with Scanner.readNextInt()?

Voidpaw
  • 910
  • 1
  • 5
  • 18
0

I got it, this is solution you are looking for:

public class InputTypeMisMatch {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int custID=0;
        System.out.println("Please enter a number");
        while (!input.hasNextInt()) {
            System.out.println("Please enter a number");
            input.next();
        }
        custID = input.nextInt();
    }
}