addCruise() — 找不到元素异常:未找到任何行。

huangapple go评论66阅读模式
英文:

addCruise() -- NoSuchElementException: No line found

问题

不再重温旧话题,但我正在为课程项目工作,而在一个特定的段落中我一再遇到这个错误,在这个段落中,我有其他各种以相同格式编写的代码部分,但这些代码并没有给我带来任何麻烦。

public static void addCruise() {

    Scanner newCruiseInput = new Scanner(System.in);
    System.out.print("Enter the name of the new cruise: ");
    String newCruiseName = newCruiseInput.nextLine();

    // 验证是否已存在同名的航线
    for (Cruise eachCruise: cruiseList) {
        if (eachCruise.getCruiseName().equalsIgnoreCase(newCruiseName)) {
            System.out.println("A cruise by that name already exists. Exiting to menu...");
            return; // 退出 addCruise() 方法的处理
        }
    }

    // 获取邮轮名称
    Scanner cruiseShipInput = new Scanner(System.in);
    System.out.print("Enter name of cruise ship: ");
    String cruiseShipName = cruiseShipInput.nextLine();
    cruiseShipInput.close();

    // 获取出发港口
    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)

为什么我会在这里遇到这个异常,而在程序的其他任何地方都没有?其他一切都经过测试并且按预期工作,但这个特定的输入正在变成一个头疼的问题。

另外,谅解我的错误格式,编辑器几乎不愿合作。

英文:

Not to revisit old topics, but I'm working on a project for a course and I'm repeatedly encountering this error at one particular segment, where I've got various other bits of code in the same format that are giving me no grief whatsoever.

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();

So, as above, I've got no trouble with anything up until that cruiseDepartureInput scanner. But before I get to provide the input for that line, Eclipse is throwing the error, which in full reads:

Enter cruise's departure port: Exception in thread "main" java.util.NoSuchElementException: No line found

  • at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
  • at Driver.addCruise(Driver.java:295)
  • at Driver.main(Driver.java:38)

Why would I be facing this exception here, but not anywhere else in the rest of the program? Everything else tested and functioned as expected, but this particular input is turning into a headache.

Also, forgive my error formatting, best I could do with how little the editor wanted to cooperate

答案1

得分: 0

删除此行,您会注意到您的问题将消失(目前):

cruiseShipInput.close();

问题在于您关闭了 System.in 流,这意味着您不能再获取任何输入。因此,当您尝试使用 System.in 启动新的扫描器时,会因为流不再存在而失败。

对于一个简单的项目,正确的方法是不要关闭扫描器,或者更好的做法是只创建一个在整个项目中都可以使用的扫描器:

public static void addCruise() {

    // 创建一个可以在整个项目中使用的单个扫描器。
    // 如果您有多个需要使用输入的类,那么需要创建一个包装类来管理扫描器输入
    Scanner inputScanner = new Scanner(System.in);

    // 现在进行其余的输入
    System.out.print("输入新邮轮的名称:");
    String newCruiseName = inputScanner.nextLine();

    // 验证是否已经存在同名的邮轮
    for (Cruise eachCruise : cruiseList) {
        if (eachCruise.getCruiseName().equalsIgnoreCase(newCruiseName)) {
            System.out.println("已经存在同名的邮轮。退出到菜单...");
            return; // 退出 addCruise() 方法处理
        }
    }

    // 获取邮轮名称
    System.out.print("输入邮轮名称:");
    String cruiseShipName = inputScanner.nextLine();

    // 获取出发港口
    System.out.print("输入出发港口:");
    String departPort = inputScanner.nextLine();
}
英文:

Remove this line and you will notice that your issue will go away (for now):

cruiseShipInput.close();

The problem here is that you are closing the System.in stream, which means you can no longer take any input. So when you attempt to start a new scanner using System.in it will fail because the stream no longer exists.

For a simple project the correct answer is to not close the scanner, or better yet, only create a single scanner that you use throughout your entire project:

public static void addCruise() {

    //Create a single scanner that you can use throughout your project.
    //If you have multiple classes then need to use input then create a wrapper class to manage the scanner inputs
	Scanner inputScanner = new Scanner(System.in);

    //Now do the rest of your inputs
	System.out.print("Enter the name of the new cruise: ");
	String newCruiseName = inputScanner.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
    System.out.print("Enter name of cruise ship: ");
    String cruiseShipName = inputScanner.nextLine();
    
    // Get port of departure
    System.out.print("Enter cruise's departure port: ");
    String departPort = inputScanner.nextLine();

huangapple
  • 本文由 发表于 2020年8月18日 22:09:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63470457.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定