Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scanner.reset() doesn't work

Tags:

java

This piece of code is supposed to get an integer number from user and then finish the program. If the user inputs an invalid number, it asks user again.

After catching exception, it uses Scanner.reset() to reset the scanner, but it doesn't work. and it re-throws previous exception.

Scanner in = new Scanner(System.in);
while (true) {
    try {
        System.out.print("Enter an integer number: ");
        long i = in.nextLong();
        System.out.print("Thanks, you entered: ");
        System.out.println(i);
        break;
    } catch (InputMismatchException ex) {
        System.out.println("Error in your input");
        in.reset(); // <----------------------------- [The reset is here]
    }
}

I thought Scanner.reset() will reset everything and forget the exception. I put it before asking the user for a new input.

If I get the point wrong, what is the right way?

like image 500
masoud Avatar asked Apr 12 '13 13:04

masoud


People also ask

How do I flush Scanner buffer?

nextLine() to clear the buffer, which works for single-line input. As comments point out, however, sometimes System.in input can be multi-line. You can instead create a new Scanner object where you want to clear the buffer if you are using System.in and not some other InputStream. in = new Scanner(System.in);

Why my Scanner in Java is not working?

The reason for your problem is that following the preceding nextInt() , you're still on the same line, and nextLine() returns the rest of the current line. That is, nextLine() did not block for your input, because the current line still has an empty string remaining.

What does Scanner reset do in Java?

Java Scanner reset() Scanner reset in Java, as the name suggests, resets the scanner object. In many programming interview problems, we need to be comfortable with different ways to handle the input.


2 Answers

You misunderstood the purpose of the reset method: it is there to reset the "metadata" associated with the scanner - its whitespace, delimiter characters, and so on. It does not change the state of its input, so it would not achieve what you are looking for.

What you need is a call of next(), which reads and discards any String from the Scanner:

try {
    System.out.print("Enter an integer number: ");
    long i = in.nextLong();
    System.out.print("Thanks, you entered: ");
    System.out.println(i);
    break;
} catch (InputMismatchException ex) {
    System.out.println("Error in your input");
    in.next(); // Read and discard whatever string the user has entered
}

Relying upon exceptions to catch exceptional situations is OK, but an even better approach to the same issue would be using has... methods before calling the next... methods, like this:

System.out.print("Enter an integer number: ");
if (!in.hasNextLong()) {
    in.next();
    continue;
}
long i = in.nextLong();
System.out.print("Thanks, you entered: ");
System.out.println(i);
break;
like image 103
Sergey Kalinichenko Avatar answered Sep 21 '22 14:09

Sergey Kalinichenko


Per Scanner.reset() javadoc, the method only "resets" locale, radix and delimiter settings. It does not do anything to the data it already read.

like image 30
david a. Avatar answered Sep 21 '22 14:09

david a.