Java Scanner Error : java.util.NoSuchElementException: No line found -- java.base/java.util.Scanner.nextLine(Scanner.java:1651))

Java Scanner Error : java.util.NoSuchElementException: No line found -- java.base/java.util.Scanner.nextLine(Scanner.java:1651))

我是 java 和 programmin 的初学者,所以这是一个文件的完整代码 reader 计算单词或显示文本文件内容的程序,我想获取用户输入我使用 if 语句指示的命令,但由于以下错误,String printFileCommand = scan.nextLine(); 无法正常工作:


    package com;
    
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class FileReader {
      public static void main(String[] args) throws FileNotFoundException {
        Scanner scanTwo = new Scanner(System.in);
        System.out.println("Please Enter Your File Path");
        String filePath = scanTwo.nextLine(); 
        scanTwo.close(); 
        File fileInput = new File(filePath);
        Scanner fileScanner = new Scanner(fileInput);
        System.out.println(fileScanner.nextLine());
        fileScanner.close();
        System.out.println("Commands: PRINT.FILE --> Prints all file    COUNT.WORDS --> Counts all words");
        System.out.println("Type Command:");
        
        Scanner scan = new Scanner(System.in);
        String printFileCommand = scan.nextLine();   <----ERROR HERE
        scan.close();
         
         
         if (printFileCommand.contains("PRINT.FILE")) {
           while (fileScanner.hasNextLine()) {
             System.out.println(fileScanner.nextLine());
            }
          } else if (printFileCommand.contains("COUNT.WORDS")) {
            int wordCount = 0;
            while (fileScanner.hasNext()) {
              String fileWords = fileScanner.next();
              wordCount++;
              // System.out.println(wordCount); 
            }
            System.out.println(wordCount);
          } 
          else {
            System.out.println("COMMAND INVALID!");
          }
        } 
      }
    ```

**Terminal Output:**

PS C:\Users\DR\Desktop\FIRST REAL PROGRAMMING>  c:; cd 'c:\Users\DR\Desktop\FIRST REAL PROGRAMMING'; & 'c:\Users\DR\.vscode\extensions\vscjava.vscode-java-debug-0.30.0\scripts\launcher.bat' 'C:\Program Files\AdoptOpenJDK\jdk-15.0.1.9-hotspot\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\DR\AppData\Roaming\Code\User\workspaceStorage8dc35931a3067a355426e5ceeeee32\redhat.java\jdt_ws\FIRST REAL PROGRAMMING_e263b9bc\bin' 'com.FileReader'
Please Enter Your File Path
E://texttwo.txt
This is my text file.
Commands: PRINT.FILE --> Prints all file    COUNT.WORDS --> Counts all words
Type Command:
Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at com.FileReader.main(FileReader.java:21)
PS C:\Users\DR\Desktop\FIRST REAL PROGRAMMING>

So why is `String printFileCommand = scan.nextLine();` not working? I tried alot, but its not working...

请检查这个问题: NoSuchElementException - class Scanner

如果您删除以下代码,您的代码将有效:

  scanTwo.close();

或者删除更好:

  Scanner scan = new Scanner(System.in);

并使用 scanTwo 进行阅读(但您不必使用 scanTwo.close() 关闭扫描仪)。

但我建议您阅读这些答案以了解其工作原理。

它不起作用,因为您的 System.in 流已关闭。 您可以检查它,例如 System.out.println(System.in.available());,您将看到:

Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:159)
at java.io.BufferedInputStream.available(BufferedInputStream.java:410)

您按以下顺序关闭它:scanTwo.close();

我自己仍在努力理解 Java,但我认为您并不完全需要创建和使用多个扫描器来收集数据。由于您正在搜索文件创建的字符串,因此从技术上讲,您可以执行以下操作:

扫描仪扫描仪=新扫描仪(System.in);

字符串文件路径 = scanner.nextLine();

对于其他一些扫描仪,您可以保留,因为您专门在扫描仪中调用 fileInputs,但是当向用户询问数据时,我建议只使用一个扫描仪源,但要有类似于最后一行的内容我分享的代码作为更新代码的模板!如果我误解了什么,非常欢迎您告诉我。谢谢!