Try-Catch inside While Loop

怎甘沉沦 提交于 2019-12-11 18:52:55

问题


The code below asks the user how many racers he/she would like.

while (true) { // loops forever until break
    try { // checks code for exceptions
        System.out.println("How many racers should" + " participate in the race?");
        amountRacers = in.nextInt();
        break; // if no exceptions breaks out of loop
    } 
    catch (InputMismatchException e) { // if an exception appears prints message below
        System.err.println("Please enter a number! " + e.getMessage());
        continue; // continues to loop if exception is found
    }
}

If a number is entered at amoutnRacers = in.nextInt(); the code breaks out of the loop and the rest of the program runs fine; however, when I enter something such as "awredsf" it should catch that exception, which it does. Instead of prompting the user again it loops continuously, which to me does not make sense.

The program prints like this when looping continuously:

How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race? How many racers should participate in the race?Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null Please enter a number! null ...

I do not understand what is going on amountRacers = in.nextInt(); so why is the user not able to enter a number?


回答1:


Just add input.next() once you catch InputMismatchException.

catch (InputMismatchException e) { //if an exception appears prints message below
    System.err.println("Please enter a number! " + e.getMessage());
    input.next(); // clear scanner wrong input
    continue; // continues to loop if exception is found
}

You need to clear the wrong input, which scanner automatically does not.




回答2:


You may need to create a Scanner class for getting standard input streamed from the keyboard. You should have a statement somewhere in your code that creates an instance of a Scanner class like: Scanner in = new Scanner(System.in); so the " in " variable in your statement: amountRacers = in.nextInt(); waits and scans for entered input from the keyboard and stores it.




回答3:


Today i solved this problem :-) This is my code. I think that i help

public int choice () throws Exception{
    Scanner read = new Scanner(System.in));
    System.out.println("Choose the option from the upper list");
    int auxiliaryChoiceMenu = 5;
    int auxiliaryVariable = -1;
    boolean auxiliaryBoolean = false;
    while (!auxiliaryBoolean) {
        try {
            auxiliaryVariable = read.nextInt();
            read.nextLine();
        } catch (Exception e) {
            System.out.println("incorrect data, try again"+e);
            read.nextLine();
            continue;
        }

        if (auxiliaryVariable<0 || auxiliaryVariable>auxiliaryChoiceMenu){
            System.out.println("incorrect data, try again");
        } else {
            auxiliaryBoolean = true;
        }
        choiceMenu = auxiliaryVariable;

    }
        return choiceMenu;
        //choicemenu is a external variable
}


来源:https://stackoverflow.com/questions/24857070/try-catch-inside-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!