问题描述
我是java和programmin的初学者,所以这是一个计算字数或显示文本文件内容的文件阅读器程序的完整代码,我想为我使用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\workspaceStorage\458dc35931a3067a355426e5ceeeee32\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...
解决方法
它不起作用,因为您的 System.in 流已关闭。
您可以检查它,例如 N = int(input())
n = 1
k = 1
while n < N:
print(n)
k = k + 1
n = k ** 2
,您将看到:
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)
我自己仍在尝试了解 Java,但我认为您并不完全需要创建和使用多个扫描器来收集数据。由于您正在搜索文件创建的字符串,因此从技术上讲,您可以执行以下操作:
Scanner 扫描仪 = new Scanner(System.in);
String filePath = Scanner.nextLine();
对于其他一些扫描仪,您可以保留,因为您在扫描仪中专门调用了 fileInputs,但是当向用户询问数据时,我建议仅使用一个扫描仪源,但使用类似于最后一行代码的内容作为更新代码的模板共享!如果我误解了什么,非常欢迎你告诉我。谢谢!
,请检查这个问题: NoSuchElementException - class Scanner
如果您删除代码,您的代码将起作用:
>>>dragon1 = Dragon('Smaug')
>>>dragon1.also_called
['flying serpent','snake with wings']
或者更好地去除:
scanTwo.close();
并使用 scanTwo 进行阅读(但您不必使用 scanTwo.close() 关闭扫描仪)。
但我建议您阅读这些答案以了解其工作原理。