'java.util.NoSuchElementException occurs for some reason
I've run this code. Description, this code is about choosing a choice what to do. Error Occurs in every input.next()
public static void showItem() throws IOException {
deleteInformation delete = new deleteInformation();
showInformation info = new showInformation();
info.showAllItemInfo(); //Just show all item (No scanner here)
Scanner input = new Scanner(System.in);
System.out.print("1.Delete an information | 2.Go back to previous | : ");
int choice = 2;
choice = input.nextInt();
while(choice < 1 || choice > 2) {
System.out.print("Please type between 1-2 : ");
input.nextLine();
choice = input.nextInt();
}
if(choice == 1) {
int wanted = 0;
System.out.print("Please type what number you want to delete : ");
wanted = input.nextInt();
while(wanted < 1 || wanted > info.countAllItem()) {
System.out.print("Please type between 1-" + info.countAllItem() + " : ");
wanted = input.nextInt();
}
delete.deleteItemNum(wanted);
chooseOption();
}
else {
chooseOption();
}
input.close();
}
Then this is how an error occurs.
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 LostItemService.showItem(LostItemService.java:216)
at LostItemService.chooseOption(LostItemService.java:130)
at LostItemService.main(LostItemService.java:8)
I tried to put input.nextLine(); on both before and after nextInt() But It didn't figured. I am finding a way to fix it. Please help ;w;. Appreciate it.
About chooseOption method
public static void chooseOption() throws IOException {
int choice = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please choose what do you want to do");
System.out.println("1.Put found item without any person information");
System.out.println("2.Insert your lost item and your information");
System.out.println("3.View existed information");
System.out.println("4.Close this program");
choice = input.nextInt();
while(choice < 1 || choice > 4) {
System.out.print("Please type between 1-4 : ");
choice = input.nextInt();
}
System.out.println("\n-----------------------------------------------------------------------------");
System.out.println();
if(choice == 1) {
saveItem();
}
else if(choice == 2) {
savePerson();
}
else if(choice == 3) {
System.out.println("\nPlease choose what do you want to do about information");
System.out.println("1.Show all item in system.");
System.out.println("2.Show all people who lost item.");
choice = input.nextInt();
while(choice < 1 || choice > 2) {
System.out.print("Please type between 1-2 : ");
choice = input.nextInt();
}
input.nextLine();
input.close();
switch(choice) {
case 1 :
//input.close();
showItem();
break;
case 2 :
//input.close();
showPerson();
break;
default : System.out.println("What?");
}
}
else {
System.out.println("Turning off program...");
//input.close();
System.exit(0);
}
input.close();
}
The main method is nothing than just called chooseOption method.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
