I'm new to java but I've experimented a lot with this particular program and I've hit a wall. The purpose of this program is to catch if the user inputs a value exceeding the variable limit and also to validate that the input satisfies my specified range.
- If the input exceeds the variable limit without a while loop, the program exits.
- If the input exceeds the variable limit with a while loop, it loops infinitely.
I've tried using a while loop and my three outcomes are this:
- The user inputs a programmer-specified valid number and all is well.
- The user inputs a number above 100 and is prompted to try again...
- The user exceeds the variable limit and the program goes into an infinite loop.
If I put an escape in the catch block, it does the same as if the while was not there. I want the program to print the error but allow the user to retry a value.
while(!success){
try{
sh = sc.nextShort();
while(sh < 1 || sh > 100){
System.out.print("You must enter a value between 1-100: ");
sh = sc.nextShort();
} //close try
System.out.println("Great job!");
success = true; //escape the while
}catch{Exception ex){
System.out.println("ERROR: " + ex);
success = false; //causes infinite loop... I understand
} //close catch
} //close while