java - 主线程中的异常 java.util.NoSuchElementException

标签 java

我正在从事这个项目,以提高我的 Java 技能。我的目标是编写一个程序,从指定的文档或文本文件中读取行(取决于用户想要打开的文件;分别为 int 2 或 1。),然后要求用户输入其文档名称(不带文件扩展名) ),然后读取文档或文本文件中的第一行。我希望它按照用户的需要多次执行此操作。但在执行代码时我不断收到 NoSuchElementException

public class switches {
    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        System.out.println("How many files would you like to scan?");
        System.out.println("Enter # of files to scan: ");
        int countInput = input.nextInt();
        input.close();

        for (int count = 0; count < countInput;) {
            System.out.println("Please enter a file name to scan. ");
            System.out.println("1 for .txt, 2 for .doc");
            Scanner keyboard = new Scanner(System.in);
            int choice = keyboard.nextInt();

            switch (choice) {
            default: {
                do {
                    System.out.println("Please pick "
                            + "either 1: txt or 2: doc");
                    choice = keyboard.nextInt();
                } while (choice != 1 && choice != 2);
            }
            case 1: {
                System.out.println("Txt file name:");
                keyboard.nextLine();
                String txtName = keyboard.nextLine();
                File openTxtFile = new File("C:/Users/Hp/Documents/" + txtName
                        + ".txt");
                Scanner firstTxtLine = new Scanner(openTxtFile);
                String printedTxtLine = firstTxtLine.nextLine();
                firstTxtLine.close();
                System.out.println("The first line " + "of your text file is: "
                        + printedTxtLine);
                keyboard.close();
                count++;
                break;
            }
            case 2: {
                System.out.println("Doc file name:");
                keyboard.nextLine();
                String docName = keyboard.nextLine();
                File openDocFile = new File("C:/Users/Hp/Documents/" + docName
                        + ".doc");
                Scanner firstLine = new Scanner(openDocFile);
                String printedDocLine = firstLine.nextLine();
                firstLine.close();
                System.out.println("The first line"
                        + " of your word document is: " + printedDocLine);
                keyboard.close();
                count++;
                break;
            }
            }
        }
    }
}

最佳答案

如果您删除第 14 行的 input.close(); 行。这应该可以解决您的问题。根据the documentation ,它将抛出 NoSuchElementException - “如果输入已用尽”。

关于java - 主线程中的异常 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14764337/

相关文章:

java - 将文件从一个文件夹复制到另一个文件夹,并使用旧文件名+时间戳在新文件夹中重命名该文件

Java字符到数字编码的转换

java - 为什么我在使用Junit测试Class时在第二种方法中得到 “null point error”?

JavaFX IllegalArgumentException

java - jsf中可以显示的行数限制

java - 如何保持运行几分钟的程序的连接

java - Grails:如何从字符串中调用服务和服务名称

java - 如何通过eclipse将引用库推送到github?

java - 多次声明变量会减慢执行速度吗?

java - 在 Android 中使用 JCIFS