Scanner - java.util.NoSuchElementException

2019-03-30 04:22发布

Does anyone see a problem with this? First input works fine, but after the first loop, it doesn't ask to enter a value again. How do I fix this?

    int value;
    while(true)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a value");
        try 
        {
            value = scan.nextInt();
            System.out.println("value provided is: " + value);
            scan.nextLine(); // consumes "\n" character in buffer
        }
        catch(InputMismatchException e) // outputs error message if value provided is not an integer
        {
            System.out.println("Incorrect input type. Try again.");
            continue; // restarts while loop to allow for re-entering of a valid input
        }
        scan.close();
    }

3条回答
迷人小祖宗
2楼-- · 2019-03-30 04:59

At the end of the while loop you have written scan.close(). This will close the scanner preventing any further scans. Removing that statement would ensure your while loop keeps asking you for the number(will be an infinite loop in your case)

Also, scan.nextInt() in effect ignores all new line and waits till you actually input a number and hit enter. So, scan.nextLine() can be omitted. You need that only in case where you use scan.nextLine() to fetch the value entered. In that case, the new line character is also read as an input, as a result of which you need an extra nextLine() call to consume it.

查看更多
倾城 Initia
3楼-- · 2019-03-30 05:02

Move scan.close(); to outside the while loop.

Also you don't have to construct a new Scanner on each iteration. Move the declaration to outside the loop as well.


When close the Scanner, this closes the System.in input stream.

So now when you try to instantiate it again, it doesn't find any opened stream and you'll get that exception.

查看更多
神经病院院长
4楼-- · 2019-03-30 05:07

When you do scan.close(), it closes the underlying System.in stream. So in the next iteration it will not have anything to read.

For example:

import java.io.IOException;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        int value;
        while (true) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter a value");
            try {
                value = scan.nextInt();
                System.out.println("value provided is: " + value);
                scan.nextLine(); // consumes "\n" character in buffer
            } catch (InputMismatchException e) // outputs error message if value
                                                // provided is not an integer
            {
                System.out.println("Incorrect input type. Try again.");
                continue; // restarts while loop to allow for re-entering of a
                            // valid input
            }
            scan.close();
            try {
                int x = System.in.read();
                System.out.println(x);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

OUTPUT:

Enter a value
10
value provided is: 10
Enter a value
java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:162)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:206)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
    at Main.main(Main.java:24)
Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:907)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Main.main(Main.java:12)

Check this question for more:

Is it safe not to close a Java Scanner, provided I close the underlying readable?

查看更多
登录 后发表回答