Asking for input after catching an exception
Asked Answered
A

3

6

I want the user to enter a number which is scanned by the following code:

scanner.nextInt();

If a user enters a string instead, the program throws InputMismatchException, which is obvious. I want to catch the exception in such a way that the program prompts the user to enter an input until the user enters an integer value.

Scanner scanner = new Scanner(System.in);
while(true) {
    try {
        System.out.println("Please enter a number: ");
        int input = scanner.nextInt();
        System.out.println(input);
        //statements
        break;
    }
    catch(InputMismatchException | NumberFormatException ex ) {
        continue;
    }
}

This code creates an infinite loop if a string is entered.

Applicatory answered 30/8, 2012 at 6:45 Comment(2)
You may need to reset the Scanner (I've no real experience with the class, but I've seen it done else where), try scanner.reset() before the continueQuincentenary
You might want to take a look at <#3059833>Transfusion
T
3

Put Scanner scanner = new Scanner(System.in); within your while loop.

Scanner scanner;
while(true) {    
    try {
        System.out.println("Please enter a number: ");
        scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        System.out.println(input);
        //statements
        break;
    }
    catch(InputMismatchException | NumberFormatException ex ) {
        System.out.println("I said a number...");
    }
}
Tiercel answered 30/8, 2012 at 6:49 Comment(2)
what you said works but I have defined the scanner object out side the method itself. In other words, it's a class variable. Is there a way to do so without creating the object within the try-catch statement?Applicatory
Of course, just reinstanciate the same object each loop. See the edit.Tiercel
A
4

The answer to my problem is as follows:

Scanner scanner = new Scanner(System.in);
while(true) {
    try {
        System.out.println("Please enter a number: ");
        int input = scanner.nextInt();
        System.out.println(input);
        //statements
        break;
    }
    catch(InputMismatchException | NumberFormatException ex ) {
        scanner.next();//new piece of code which parses the wrong input and clears the //scanner for new input
        continue;
    }
}
Applicatory answered 31/8, 2012 at 4:54 Comment(0)
T
3

Put Scanner scanner = new Scanner(System.in); within your while loop.

Scanner scanner;
while(true) {    
    try {
        System.out.println("Please enter a number: ");
        scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        System.out.println(input);
        //statements
        break;
    }
    catch(InputMismatchException | NumberFormatException ex ) {
        System.out.println("I said a number...");
    }
}
Tiercel answered 30/8, 2012 at 6:49 Comment(2)
what you said works but I have defined the scanner object out side the method itself. In other words, it's a class variable. Is there a way to do so without creating the object within the try-catch statement?Applicatory
Of course, just reinstanciate the same object each loop. See the edit.Tiercel
R
0

How about this?

while(true) {    
    try {
        System.out.println("Please enter a number: ");
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        System.out.println("\n\nEntered number is : " + input);
        break;
    } catch(InputMismatchException | NumberFormatException ex ) {
        System.out.println("\n\nInput was not a number. Please enter number again : ");
    } catch(Exception e ) {
        System.out.println("\n\nException caught :: " + e);
    }
}

I have also removed continue syntax as those are not needed.

Retroflex answered 30/8, 2012 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.