开发者

Error in while loop. It will not stop Java

开发者 https://www.devze.com 2023-02-23 00:55 出处:网络
Hey guys I have the following while loop which seems not to stop. It\'s supposed to ask the user for an hour. I\'m trying to catch the event where the user does not enter a number.

Hey guys I have the following while loop which seems not to stop. It's supposed to ask the user for an hour. I'm trying to catch the event where the user does not enter a number.

i.e. Enter hour: foo You did not enter a valid value

It should then allow the user to enter a value for hour again, but it keeps printing the error message over and over

    private static void setTime(Clock clock){
        int hours = -1;
        int minutes = -1;
        int seconds = -1;

        Scanner scanner = new Scanner(System.in);

        while(true)
        {
            try{
                System.out.print("Enter hours: ");
                hours = scanner.nextInt();          
            }
            catch(NumberFormatException nfe){
                System.out.println("Input was not an integer, please try again");
                continue;
            }
            catch(InputMismatchException ims){
                System.out.println("Input was not an integer, please try again");
开发者_开发技巧                continue;
            }
            break;
        }
}


Move Scanner scanner = ... into the while loop.


From the documentation :

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.

You must read the wrong answer before restarting the loop, otherwise it will read the same thing again and again.

And according to the documentation nextInt() never return a NumberFormatException, so there's no need to test against it.

You can also use hasNextInt() like this :

while(true) {
    if(scanner.hasNextInt()) {
        hours = scanner.nextInt();
        break;
    } else {
        scanner.next();
        System.out.println("You must enter an integer");
    }
}


You can add scanner.nextLine(); after printing the exception.

To be clear:

try{
                System.out.print("Enter hours: ");

                hours = scanner.nextInt();          
            }
            catch(NumberFormatException nfe){
                System.out.println("Input was not an integer, please try again");
                scanner.nextLine();
                continue;
            }
            catch(InputMismatchException ims){
                System.out.println("I--nput was not an integer, please try again");
                scanner.nextLine();
                continue;
            }
            break;


Have you tried reading from the Scanner as a string first, and then attempting to parse it as an integer?

e.g.

String line = scanner.nextLine();
hours = Integer.parseInt(line);


It's because scanner.nextInt() moves to the next token ONLY if the parsing has been successful (see the Javadoc).

0

精彩评论

暂无评论...
验证码 换一张
取 消