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?
Yes, that could be the issue. If that
Scannerhas the same source (System.in?) askband isclose()d, that closes the underlying stream, andkbcannot get input anymore.