Error handling bad user input (InputMismatchException help)
I spent about 6 hours working on this program for class without testing it
once. Everything seemed to click fine except for my user input error
handling:
int userVoteSelection;
while(true)
{
try
{
userVoteSelection = scanner.nextInt();
if (userVoteSelection < 1 || userVoteSelection > 4)
System.out.println("That is not a valid selection, try
again: ");
else
break; //user input is fine, break from loop
}
catch(InputMismatchException e)
{
System.err.println("Enter digits only, try again: ");
}
}
I recall doing this in my previous year of school, but now when the
exception is caught, it prints the error message endlessly (I tried
commenting out the while block and it still did it, I assume because the
error hasn't been "thrown" or dealt with?). Any help is appreciated.
No comments:
Post a Comment