Data Validation and Scanners in Java

183 views Asked by At

I have a question regarding data validation and scanners.The following piece of code checks userinput.Anything other than an integer is not allowed and the user is asked to re-enter a value.My question is that the code works only if the scanner is declared within the while loop.The program executes infinitely if the scanner is declared outside.Why is that?Thanks.

int UserInp;
    boolean dataType=false;
    while(dataType==false)
    {
        Scanner sc=new Scanner(System.in);
        try
        {

    System.out.print("\nEnter a number: ");
    UserInp=sc.nextInt();

    dataType=true;
        }
        catch(Exception JavaInputMismatch)
        {

            System.out.println("Option not available.Try again.");

        }

    }
3

There are 3 answers

0
Purag On BEST ANSWER

Interesting problem!

What happens is that the Scanner attempts to translate the non-integer to an integer, and realizes it can't -- so it throws an InputMismatchException. However, it only advances past the token if the translation was successful.

Meaning, the invalid string is still in the input buffer, and it will fail the translation every single time you loop and try to call nextInt(). You never set dataType to true, and so you loop infinitely.

To see this in action, you can grab the arbitrary content in your catch block and print it out:

catch(Exception JavaInputMismatch){
    System.out.println( sc.next() );
    System.out.println("Option not available.Try again.");
}

Indeed, after invalid input, we get the following:

Enter a number: hello
hello
Option not available.Try again.

Enter a number:

And we don't loop infinitely. This is because the call to next() grabbed the value from the input buffer and advanced the scanner's pointer into that buffer to the next slot, which is now empty. So nextInt() will wait for input in that case.

Oh, and the reason it works fine if you initialize in the loop is that the scanner will always start reading input fresh; scanners don't share state across instances, so the "hello" that was in the buffer for the previous iteration isn't in the buffer for the next one due to the reinitialization.

Technically, it's still in the standard input buffer, but the scanner's pointer into that buffer is beyond the invalid string because it will start reading any new input, not existing input.

0
Srini On

To add to Purag's answer, you could alternatively use nextLine() to advance the Scanner past the current line.

So your catch block will look like this:

catch(Exception JavaInputMismatch)
    {
        System.out.println("Option not available.Try again.");
        sc.nextLine();
    }
0
SASIKUMAR SENTHILNATHAN On

Tricky question.

You may get it!

The answer is simple. The Scanner object is kept live till the end of the execution as it is declared outside the while loop. Look this problem in the memory level.

The Scanner object is kept live so while entering the loop next time still the value(String value) will be there in Scanner object and it doesn't listens keyboard as the exception is already thrown.So the loop keeps going.

Note : The next() method in Scanner class will accept all the types of keyboard input but not the rest of the methods such as nextInt(), nextFloat() etc..,