我已经尝试了一些方法来检测我的代码中的 EOF,但它仍然无法正常工作。我试过使用 BufferedReader、Scanner 和使用 char u001a 来标记 EOF,但对我的代码仍然没有任何意义。这是我最后的代码:
Scanner n=new Scanner(System.in);
String input;
int counter=0;
while(n.hasNextLine())
{
input=n.nextLine();
char[] charInput=input.toCharArray();
for (int i = 0; i < input.length(); i++) {
if(charInput[i]=='"')
{
if(counter%2==0)
{
System.out.print("``");
}
else
{
System.out.print("''");
}
counter++;
}
else
{
System.out.print(charInput[i]);
}
}
System.out.print("\n");
}
该程序应该在已经到达 EOF 时停止,但我不知道为什么,由于某些原因它继续运行并导致运行时错误。请帮忙。顺便说一句,我是新来的,如果我的问题不是很清楚,我很抱歉,先谢谢你:)
原文由 Bertha Alan 发布,翻译遵循 CC BY-SA 4.0 许可协议
它一直在运行,因为它没有遇到 EOF。在流的末尾:
read()
返回-1。read(byte[])
返回 -1。read(byte[], int, int)
返回-1。readLine()
返回空值。readXXX()
对于任何其他 X 投掷EOFException
。Scanner.hasNextXXX()
对任何 X 返回 false。Scanner.nextXXX()
抛出NoSuchElementException
对于任何 X。除非您遇到其中之一,否则您的程序没有遇到流结束。注意
\u001a
是 Ctrl/z。不是 EOF。 EOF 不是字符值。