Scanner error that I can't figure out: NoSuchE

2019-06-03 10:37发布

问题:

It's crashing on the third line inside the do-while loop, and doesn't wait for my input:

 input = kb.nextInt();

Stack trace:

Exception in thread "main" java.util.NoSuchElementException

at java.util.Scanner.throwFor(Unknown Source)

at java.util.Scanner.next(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at java.util.Scanner.nextInt(Unknown Source)

at main.MainDriver.main(MainDriver.java:50)

Relevant code:

do
    {
        displayFullMenu();
        System.out.print("Selection: ");
        input = kb.nextInt();

        switch (input)
        {
        //Create new survey
        case 1:     currentSurvey = new Survey();
                    break;

        //Display current survey            
        case 2:     currentSurvey.display();
                    break;

        //Save current survey           
        case 3:     saveSurvey(currentSurvey);
                    break;

        //Load a survey
        case 4:     currentSurvey = loadSurvey();
                    break;

        //Modify a survey
        case 5:     currentSurvey.modify();
                    break;

        /*******************Test Functions*******************/

        //Create new test
        case 6:     currentSurvey = new Test();
                    break;

        //Display current test
        case 7:     currentSurvey.display();
                    break;

        //Save current test
        case 8:     saveSurvey(currentSurvey);
                    break;

        //Load a test
        case 9:     currentSurvey = loadTest();
                    break;

        //Modify a test
        case 10:    currentSurvey.modify();

        default:    System.out.println("Invalid choice. Please make a valid choice: ");
                    input = kb.nextInt();
                    System.out.println();
        }
    } while (input != 99);
    kb.close();

It crashes after I choose option 9. It saves the file correctly, then goes back to the top of the loop, and crashes at the previously mentioned line. I want it to ask for more input.

What gives?

回答1:

When I choose option 8, in saveSurvey(), it has to create a new Scanner (in that method) because all of this is inside my main method. Could that be the issue?

Yes, that could be the issue. If that Scanner has the same source (System.in?) as kb and is close()d, that closes the underlying stream, and kb cannot get input anymore.



回答2:

I figured it out.

The whole issue was caused by me not creating a static scanner in main - and when I needed it in other methods outside of main, I created new ones.

Instead of

public class MainDriver
{
    public static Scanner kb = new Scanner(System.in);
    public void main(String[] args) throws IOException 
   {

I had:

public class MainDriver
{
    public static void main(String[] args) throws IOException 
    {
        public static Scanner kb = new Scanner(System.in);

And then created new scanners in the other methods. At the end of those methods, I closed the Scanner. I guess it was closing the local scanner, because when I got rid of all close() statements that were inside the other methods, the issue disappeared.