读取 EOF 后使用 Scanner 类读取 Standard.In 时出现 NoSuchElementException
所以,首先,这是作业,尽管不是我的。这是我姐夫的。他向我寻求帮助,因为我是做计算机的,但我只从事 C++ 工作。他使用 System.in
将键盘输入读入文件,直到获得 EOF
。然后,他创建一个 Scanner
实例,并在该实例上调用 nextLine
来尝试获取 filename
并获取 NoSuchElementException
代码>.根据 Javadoc 的说法,这意味着没有接收到任何输入,这在使用 System.in 并在键盘上键入时返回似乎是一件奇怪的事情。我怀疑 EOF 字符在某种程度上没有被消耗。他的代码将于今晚午夜到期,并且他已经完成了其他所有事情(我建议他使用虚拟文件名并回到问题上)。
以下是他的代码:
import java.util.*;
import java.io.*;
public class FileTest
{
public static void main(String[] args)
{
createFile();
readFile();
}
public static void createFile()
{
//Variables
InputStream istream;
PrintStream ostream;
istream = System.in;
ostream = System.out;
Scanner keyboard = new Scanner(System.in);
int lastEntry = 0;
final int EOF = -1;
//Asks user for filename.
try
{
String fileName;
System.out.println("Please enter the filename of the file you want to open: ");
fileName = keyboard.next();
//Creates specified file.
File currentFile = new File(fileName);
//Checks if file already exists.
while(currentFile.exists())
{
System.out.println(fileName + " already exists");
System.out.println("Error: To prevent tis file from being overwritten please enter another file name");
fileName = keyboard.nextLine();
}
//Asks user for information they want stored in file.
try
{
ostream = new PrintStream(fileName);
System.out.println("Please enter what you would like to put in the file and press Ctrl+Z when finished: ");
//Writes information to file.
try
{
while((lastEntry = istream.read()) != EOF)
ostream.write(lastEntry);
}
catch(Exception e)
{
System.out.println("Error: " +e.getMessage());
}
}
catch(Exception f)
{
System.out.println("Error: " +e.getMessage());
}
}
finally
{
}
}
public static void readFile()
{
InputStream input;
PrintStream output;
output = System.out;
int lastEntry = 0;
final int EOF = -1;
Scanner keyboard2 = new Scanner(System.in);
//Asks user for filename.
String newFile;
System.out.println("Please enter the filename of the file you want to open: ");
newFile = keyboard2.next();
}
}
他在 newFile = Keyboard2.next()
行上收到 NoSuchElementException
错误。我浏览了一堆例子,发现人们发布了关于这个问题的帖子,但我还没有找到解决方案,所以我想我会为他赌上我的(可疑的)声誉。有人知道他如何让它发挥作用吗?
So, first off, this is homework, albeit not mine. It's my brother-in-law's. He asked me for help since I do computers, but I only work in C++. He's reading in keyboard input into a file using System.in
until he gets an EOF
. Afterwards, he creates an instance of Scanner
and calls nextLine
on the instance to try to get a filename
and gets a NoSuchElementException
. According to Javadoc, that means there's no input to be received, which seems like an odd thing to get back when using System.in
and typing on the keyboard. My suspicion is that the EOF
character is somehow not getting consumed. His code is due at midnight tonight and he's got everything else done (I advised him to use a dummy filename and to come back to the problem).
Here is his code:
import java.util.*;
import java.io.*;
public class FileTest
{
public static void main(String[] args)
{
createFile();
readFile();
}
public static void createFile()
{
//Variables
InputStream istream;
PrintStream ostream;
istream = System.in;
ostream = System.out;
Scanner keyboard = new Scanner(System.in);
int lastEntry = 0;
final int EOF = -1;
//Asks user for filename.
try
{
String fileName;
System.out.println("Please enter the filename of the file you want to open: ");
fileName = keyboard.next();
//Creates specified file.
File currentFile = new File(fileName);
//Checks if file already exists.
while(currentFile.exists())
{
System.out.println(fileName + " already exists");
System.out.println("Error: To prevent tis file from being overwritten please enter another file name");
fileName = keyboard.nextLine();
}
//Asks user for information they want stored in file.
try
{
ostream = new PrintStream(fileName);
System.out.println("Please enter what you would like to put in the file and press Ctrl+Z when finished: ");
//Writes information to file.
try
{
while((lastEntry = istream.read()) != EOF)
ostream.write(lastEntry);
}
catch(Exception e)
{
System.out.println("Error: " +e.getMessage());
}
}
catch(Exception f)
{
System.out.println("Error: " +e.getMessage());
}
}
finally
{
}
}
public static void readFile()
{
InputStream input;
PrintStream output;
output = System.out;
int lastEntry = 0;
final int EOF = -1;
Scanner keyboard2 = new Scanner(System.in);
//Asks user for filename.
String newFile;
System.out.println("Please enter the filename of the file you want to open: ");
newFile = keyboard2.next();
}
}
He's getting the NoSuchElementException
error on the newFile = keyboard2.next()
line. I've browsed my way through a bunch of examples and found people posting about having this problem, but I've yet to find a solution, so I figured I'd put my (dubious) reputation here on the line for him. Anyone know how he can get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有一个无限循环:
currentFile
未使用新文件名进行更新。至于手头的实际问题,斯蒂芬是对的,一旦您在
createFile
方法中点击EOF
,您就无法从键盘获得更多输入。在这方面,您认为EOF
没有被消耗是正确的。您需要使用某种其他类型的信号来指示输入文本的结尾(可能是文字字符串"EOF"
)。You have an infinite loop here:
currentFile
isn't being updated with the new file name.As for the actual problem at hand, Stephen is right, once you hit the
EOF
in thecreateFile
method, you can't get more input from the keyboard. In that respect, you're right that theEOF
isn't consumed. You'll need to use some other sort of signal to indicate the end of the input text (the literal string"EOF"
, perhaps).我正在阅读您的代码,我认为我发现了一个与您的问题完全无关的错误:
如果
currentFile.exists()
,您将获得无限循环,因为您不更新currentFile
在循环中,只需fileName
。尽管这可能不是您目前最关心的问题,但您仍然应该解决它以避免将来遇到问题。
解决您的问题:
他可以捕获 NoSuchElementException。在他的 catch 语句中,他可以执行一条语句做任何他想做的事情:退出方法,结束程序,任何事情!
I was reading your code and I think I found a bug completely unrelated to your question:
If
currentFile.exists()
, you're going to get an infinite loop because you don't updatecurrentFile
in your loop, justfileName
.Although this may not be your primary concern at the moment, you should still fix it to avoid running into the problem in the future.
To address your problem:
He can catch the NoSuchElementException. In his catch statement, he can execute a statement doing whatever he wants to do: exit the method, end the program, anything!
控制台上的 EOF 字符表示...文件结束。这就是 Scanner 方法调用抛出
NoSuchElementException
的原因。纯 Java 应用程序无法读取 EOF 标记之后的任何内容。
不是纯Java。理论上你可以从本机代码中做到这一点,但我怀疑这是否会被老师接受。
The EOF character on the console means ... end of file. That's why Scanner method calls are throwing
NoSuchElementException
.There in no way for a pure Java application to read anything after the EOF marker.
Not in pure Java. In theory you could do this from native code, but I doubt that would be acceptable to the teacher.