【问题标题】:addCruise() -- NoSuchElementException: No line foundaddCruise() -- NoSuchElementException: 找不到行
【发布时间】: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


    【解决方案1】:

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

    cruiseShipInput.close();
    

    这里的问题是您正在关闭System.in 流,这意味着您不能再接受任何输入。因此,当您尝试使用 System.in 启动新扫描仪时,它将失败,因为流不再存在。

    对于一个简单的项目,正确的答案是不要关闭扫描仪,或者更好的是,只创建一个您在整个项目中使用的扫描仪:

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

    【讨论】:

    • 这是我第一次了解到在任何扫描仪上调用 .close() 会完全关闭 system.in。我原以为它会简单地关闭指定 Scanner 对象的输入。不过效果很好,感谢您的快速回复
    猜你喜欢
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多