【问题标题】:"No such element" error when running a method for a second time [duplicate]第二次运行方法时出现“没有这样的元素”错误[重复]
【发布时间】:2018-05-10 03:14:38
【问题描述】:

对不起,如果这是一个愚蠢的问题,我有点编程菜鸟,并且在使用以下代码时遇到了问题。

public static void main(String[] args) throws IOException {
    addName();
    addName();      //Line "10" in error
}

public static void addName() throws IOException {
    //Initializing Variables and Scanner
    Scanner keyboard = new Scanner(System.in);
    FileWriter fwriter = new FileWriter("Data.txt", true);
    PrintWriter outputFile = new PrintWriter(fwriter);
    int numFriends;
    String firstName;
    String lastName;
    String email;

    //Get number of entries to add
    System.out.print("How many people do you wish to add? ");
    numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error

    //Prompt for info and add it to the file
    for(int i = 1; i <= numFriends; i++){
        System.out.printf("Enter the first name of person %d: ",i);
        firstName = keyboard.nextLine();
        System.out.printf("Enter the last name of person %d: ",i);
        lastName = keyboard.nextLine();
        System.out.printf("Enter the email of person %d: ",i);
        email = keyboard.nextLine();

        outputFile.println(firstName + " " + lastName + " " + email);
    }

    //Wrap things up and give a confirmation
    outputFile.close();
    keyboard.close();
    System.out.println("Data written to the file.");;
}

addName 方法运行一次时有效,但第二次运行时出现跟随错误。

How many people do you wish to add? 1
Enter the first name of person 1: s
Enter the last name of person 1: d
Enter the email of person 1: g
Data written to the file.
How many people do you wish to add? Exception in thread "main" 
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Problem1.addName(Problem1.java:25)
at Problem1.main(Problem1.java:10)

我最初认为这是来自没有在某处使用的换行符,但我似乎无法弄清楚。为什么我会收到此错误?我该如何解决?谢谢!

【问题讨论】:

    标签: java java.util.scanner filewriter printwriter


    【解决方案1】:

    您不应该在方法结束时关闭Scanner,理想情况下,您应该在整个执行过程中重复使用相同的Scanner

    通过关闭Scanner,您也关闭了它的输入源,在本例中为System.in。因此,用户无法再输入任何内容。

    以下是修正后的 sn-p:

    public static void main(String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
        addName(keyboard);
        addName(keyboard);
    }
    
    public static void addName(Scanner keyboard) throws IOException {
        FileWriter fwriter = new FileWriter("Data.txt", true);
        PrintWriter outputFile = new PrintWriter(fwriter);
        int numFriends;
        String firstName;
        String lastName;
        String email;
    
        //Get number of entries to add
        System.out.print("How many people do you wish to add? ");
        numFriends = Integer.parseInt(keyboard.nextLine());        //Line "25" in error
    
        //Prompt for info and add it to the file
        for(int i = 1; i <= numFriends; i++){
            System.out.printf("Enter the first name of person %d: ",i);
            firstName = keyboard.nextLine();
            System.out.printf("Enter the last name of person %d: ",i);
            lastName = keyboard.nextLine();
            System.out.printf("Enter the email of person %d: ",i);
            email = keyboard.nextLine();
    
            outputFile.println(firstName + " " + lastName + " " + email);
        }
    
        //Wrap things up and give a confirmation
        outputFile.close();
        System.out.println("Data written to the file.");
    }
    

    【讨论】:

    • 哇,我现在感觉很笨。非常感谢!
    • 关闭阅读器也会关闭输入源并不一定很直观,但是随着您变得更有经验,您会意识到这通常是 Java 中的默认行为(我实际上不知道任何反标准库中的示例)。如果有帮助,请随时投票/接受此答案。
    • 原则是,你负责关闭你打开的任何东西。所以,你打开了文件Data.txt;你有责任关闭它。你没有没有打开System.in;你只在它周围包裹了一个Scanner,但它已经被Java(或者,实际上是操作系统)为你打开了。所以你不应该关闭它。
    猜你喜欢
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    相关资源
    最近更新 更多