I'm trying to validate a user's input on a menu. Options are 1, 2, 3, and 4. I'm trying to handle the InputMismatchException
error when they enter in a letter. I can't see what I'm doing wrong to make my code get stuck in an infinite loop.
System.out.println("What will be your starting balance?");
double startingBalance =0;
boolean check = false;
while(!check) {
try {
startingBalance = input.nextDouble();
check = true;
}
catch (InputMismatchException e) {
System.out.println("Invalid input!");
//startingBalance = 0;
//e.printStackTrace();
//check = false;
}
}
It looks like it get into the catch part, but loops through that repeatedly instead of going back to the try. I tried doing input.nextDouble();
to clear input buffer, but did nothing. Any help would be greatly appreciated.