kaygeee
kaygeee

Reputation: 3

Validating input and preventing try/catch from exiting

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.

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

Upvotes: 0

Views: 1494

Answers (3)

Raju Sharma
Raju Sharma

Reputation: 2516

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

Upvotes: 1

Attila Gyukics
Attila Gyukics

Reputation: 108

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
    }

Upvotes: 1

washcloth
washcloth

Reputation: 2896

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

Upvotes: 0

Related Questions