简体   繁体   English

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

[英]Exception in main thread java.util.NoSuchElementException

I'm working on this project to improve my skill in Java. 我正在研究这个项目,以提高我的Java技能。 My goal is to write a program that reads the line from a specified doc or text file (depending on which the user wants to open; int 2 or 1 respectively.) and then asks the user to input their document name (without the file extension), and then reads the first line in the document or text file. 我的目标是编写一个程序,从指定的文档或文本文件中读取行(取决于用户想要打开的行;分别为int 2或1)。然后要求用户输入他们的文档名称(没有文件扩展名) ),然后读取文档或文本文件中的第一行。 I want it to do this as many times as the user wants. 我希望它能像用户想要的那样多次这样做。 But I keep getting NoSuchElementException while executing the code. 但是在执行代码时我不断收到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;
            }
            }
        }
    }
}

If you remove the line input.close(); 如果你删除行input.close(); on line 14. This should solve your problem. 在第14行。这应该可以解决您的问题。 According to the documentation , it will throw a NoSuchElementException - "if input is exhausted". 根据文档 ,它将抛出NoSuchElementException - “如果输入已用尽”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主”中的异常java.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException - Exception in thread "main" java.util.NoSuchElementException Java扫描器错误:线程“主”中的异常java.util.NoSuchElementException - Java Scanner Error: Exception in thread “main” java.util.NoSuchElementException Java中线程“main”java.util.NoSuchElementException中的异常 - Exception in thread "main" java.util.NoSuchElementException in Java java 新手 - 线程“main”中的异常 java.util.NoSuchElementException - New to java - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的Java异常java.util.NoSuchElementException运行时错误 - Java Exception in thread “main” java.util.NoSuchElementException runtime error Java错误-“线程“ main”中的异常” java.util.NoSuchElementException - Java Error - “Exception in thread ”main" java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException,Java 扫描器 - Exception in thread "main" java.util.NoSuchElementException, Java scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM