简体   繁体   中英

Exception in main thread java.util.NoSuchElementException

I'm working on this project to improve my skill in 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. I want it to do this as many times as the user wants. But I keep getting NoSuchElementException while executing the code.

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(); on line 14. This should solve your problem. According to the documentation , it will throw a NoSuchElementException - "if input is exhausted".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM