I am pretty new to Java, but know some of C and Python, hence some of Java looks alike. I have a program that worked well, until I tried to create a "main menu". I did as I've always done in the previous languages, but the program will only work ONE time through the loop, then crash.
Code in which error occurs:
while (true)
{
java.util.Scanner in = new java.util.Scanner(System.in);
System.out.println("Alternative 1. Add A New Person To Database");
System.out.println("Alternative 2. Quit The Program");
int choice = in.nextInt(); //This is where error is found! (:22)
if (choice==1)
{
choice1();
}
if (choice==2)
{
System.out.println("Look at the file text.txt");
System.exit(-1);
}
}
Error Message:
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 EgnaProgrammet.main(EgnaProgrammet.java:22)
I'm certain that something is wrong with the input since the problem occurs the second time I want to make a input in the while. Could it be because choice
already has a value?
Appriciate any help!