相关的代码部分:
Scanner kbd = new Scanner(System.in); //to get user input
while(!close) { //program will keep prompting user for selection until they close it
System.out.println(menu); //prints the menu
selection = kbd.nextLine(); //gets the menu selection from the user
仅当从方法调用返回while循环时才会发生这种情况,如果我从if语句返回而没有方法调用则不会发生错误。以下是完整的错误消息:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at class.main(class.java:29)
它指向的行是selection = kbd.nextLine();
行。
答案 0 :(得分:0)
我在这里找到了解决问题的方法!
Java Scanner Take Input, call a method, then back to read more input not working
在我的方法中发现扫描仪的其他实例导致主扫描程序出现问题。将扫描仪移到班级顶部解决了这个问题!
答案 1 :(得分:0)
@Spork
扫描仪kbd =新扫描仪(System.in); //获取用户输入
while(!close) { //program will keep prompting user for selection until they close it
System.out.println(menu); //prints the menu
if(kbd.hasNextLine(){
selection = kbd.nextLine(); //gets the menu selection from the user
}
}
让我知道它是否有帮助。