【发布时间】:2020-08-18 14:09:19
【问题描述】:
不要重温旧话题,但我正在为一门课程做一个项目,我在一个特定的部分反复遇到这个错误,在那里我有各种其他位的 相同格式的代码 这让我没有任何悲伤。
public static void addCruise() {
Scanner newCruiseInput = new Scanner(System.in);
System.out.print("Enter the name of the new cruise: ");
String newCruiseName = newCruiseInput.nextLine();
// Verify no cruise of same name already exists
for (Cruise eachCruise: cruiseList) {
if (eachCruise.getCruiseName().equalsIgnoreCase(newCruiseName)) {
System.out.println("A cruise by that name already exists. Exiting to menu...");
return; // Quits addCruise() method processing
}
}
// Get name of cruise ship
Scanner cruiseShipInput = new Scanner(System.in);
System.out.print("Enter name of cruise ship: ");
String cruiseShipName = cruiseShipInput.nextLine();
cruiseShipInput.close();
// Get port of departure
Scanner cruiseDepartureInput = new Scanner(System.in);
System.out.print("Enter cruise's departure port: ");
String departPort = cruiseDepartureInput.nextLine();
cruiseDepartureInput.close();
因此,如上所述,我对任何 直到 cruiseDepartureInput 扫描仪都没有任何问题。但在我为该行提供输入之前,Eclipse 抛出了错误,全文如下:
输入游轮的出发港口:线程“main”中的异常 java.util.NoSuchElementException:找不到行
- 在 java.base/java.util.Scanner.nextLine(Scanner.java:1651)
- 在 Driver.addCruise(Driver.java:295)
- 在 Driver.main(Driver.java:38)
为什么我会在这里遇到这个异常,而在程序的其他地方却没有?其他一切都按预期进行了测试和运行,但是这个特殊的输入变得令人头疼。
另外,请原谅我的错误格式,我能做到的最好是编辑不想合作
【问题讨论】:
标签: java java.util.scanner nosuchelementexception