0

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:

  1. The user inputs a programmer-specified valid number and all is well.
  2. The user inputs a number above 100 and is prompted to try again...
  3. 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
Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
kaygeee
  • 3
  • 1
  • I'm not sure I understand your problem but I'd suspect your problem is that first ` sh = sc.nextShort();` in the try-block. If that was all your code I as a user wouldn't know that I have to enter something so I'd wait for the program to print something and the program would wait for my input - a classical deadlock. I suspect the same happens when an error occurs, i.e. it prints the error and then starts the outer loop's body again which starts with waiting for a number to be entered. An easy "fix" would be to print something like "try again:" after printing the error. – Thomas Sep 01 '17 at 13:31
  • Possible duplicate of [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Sep 01 '17 at 13:37
  • The problem was, when an exception was thrown, it would loop the error message infinitely. I followed the post below by creating a new "Try again:" prompt and scanner in the catch. It's functional but I still have a lot of learning to do in order to understand exactly what's happening. Thanks for the advice! – kaygeee Sep 01 '17 at 14:28

3 Answers3

1

As i understand your problem , you are facing a loop of exception on passing a bigger value (something bigger like e.g "10000000000").

you have the exceptions coming as below in infinite loop:

ERROR: java.util.InputMismatchException: For input string: "10000000000"
ERROR: java.util.InputMismatchException: For input string: "10000000000"

But you want program to print a exception line and then allow user to input again.

You can do that by below a way of reading bad input (String bad_input = sc.next();) from the scanner you used.

 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
                        String bad_input = sc.next();// your bad input to allow read that exception
                  } // close catch
            } // close while

The cause of this problem can be found here:

If the translation is successful, the scanner advances past the input that matched

Raju Sharma
  • 2,406
  • 3
  • 20
  • 40
1

The problem is, that Scanner stores the whole input string and processes it. When your input is not a short value but a string it throws an exception. You log the exception in the catch and in the next circle it tries to read a short value from the same stored string, which throws exception again .... -> infinite loop.

If you create a new Scanner in the catch, it will read again from the console:

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);
            sc = new Scanner(System.in);
        } //close catch
    }
  • I tested this and it worked! I'm still not quite understanding but I've marked it as correct and I'll do more learning. Thank you! – kaygeee Sep 01 '17 at 14:18
0

If I understand your question correctly, you can get rid of your second while loop and replace it with an if. Then you can print out the value must be 1-100 and throw and exception to cause your program to go through the catch statement and print out the error you threw.

while(!success){
    try{
        sh = sc.nextShort();
        if(sh < 1 || sh > 100){
            System.out.print("You must enter a value between 1-100");
            throw Exception;
        } //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
washcloth
  • 2,322
  • 13
  • 27