我正在使用链接列表并收到此错误消息:
NoSuchElementException:找不到行
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String selection = null;
do {
System.out.println("\n\t\tMENU DISPLAY\n-----------------------------------------------");
System.out.println("a\tShow all records");
System.out.println("\nr\tRemove the current record");
System.out.println("\nf\tChange the first name in the current record");
System.out.println("\nl\tChange the last name in the current record");
System.out.println("\nn\tAdd a new record");
System.out.println("\nd\tAdd a deposit to the current record");
System.out.println("\nw\tMake a withdrawal from the current record");
System.out.println("\nq\tQuit");
System.out.println("\ns\tSelect a record from the record list to become the current record");
System.out.print("\nEnter a command from the list above, or q to quit: ");
selection = Input.nextLine();
switch(selection) {
case "a":
showAll();
break;
case "r":
removeCurrent();
break;
case "f":
changeFName();
break;
case "l":
changeLName();
break;
case "n":
addNew();
break;
case "d":
deposit();
break;
case "w":
withdraw();
break;
case "q":
System.out.println("The program will now terminate.");
System.exit(1);
case "s":
makeCurrent();
break;
case "t":
listSort();
break;
default:
System.out.println("Error. Please only enter an option from the menu.\n");
break;
}
} while(true);
}
这是我的stacktrace:
a显示所有记录
r删除当前记录
f更改当前记录中的名字
l更改当前记录中的姓氏
n添加新记录
d在当前记录中添加存款
w退出当前记录
q退出
s从记录列表中选择一条记录成为当前记录
从上面的列表中输入命令,或输入q以退出:n
输入名字:Ada
输入姓氏:Caswell
a显示所有记录
r删除当前记录
f更改当前记录中的名字
l更改当前记录中的姓氏
n添加新记录
d在当前记录中添加存款
w退出当前记录
q退出
s从记录列表中选择一条记录成为当前记录
从上面的列表中输入命令,或输入q以退出:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at bank.main(bank.java:225)
我尝试对异常进行一些研究,在将while(true)
更改为while(input.hasNextLine())
后,这似乎只会让事情变得更糟。虽然我不再收到错误,但在输入选项后input.hasNextLine
无法多次显示菜单。我该如何解决这个问题?