英文:
What scenario would create an nosuchelementexception with nextLine()?
问题
nextLine()
方法的文档说明中提到它可能会抛出NoSuchElementException
异常。但是在使用nextLine()
来获取Scanner的输入时,如下面的代码所示,实际上并没有抛出NoSuchElementException
异常。唯一发生的情况是连续按两次“Enter”键程序会直接结束。我将相同的代码提交到一个在线系统进行评估,系统同样表示该代码会抛出NoSuchElementException
异常。
有什么样的输入会导致抛出NoSuchElementException
异常?
String input = "";
Scanner sc = new Scanner(System.in);
input += sc.nextLine() + " ";
input += sc.nextLine() + " ";
System.out.println(input);
英文:
The documentation for nextLine()
says that it can throw a nosuchelementexception
. But when using the nextLine()
to get input for the Scanner as demonstrated in the following code, the nosuchelementexception
is not thrown, The only thing that happens is by pressing "Enter" two times the programs just ends. I submitted the same code for an evaluation to an online system, there also the system said that the code throws a nosuchelementexception
What sort of an input would produce a nosuchelementexception
?
String input = "";
Scanner sc = new Scanner(System.in);
input += sc.nextLine() + " ";
input += sc.nextLine() + " ";
System.out.println(input);
答案1
得分: 1
这里是一个示例,其中标准输入(即 System.in
)从另一个应用程序进行了管道传输:
echo '只有一行' | java Read
或者从文件中读取:
java Read <file_with_one_line.txt
或者使用交互式用户输入:
java Read
我正在输入一行 Ctrl+D
这些示例假设您已经将您发布的代码(包装到一个名为 Read
的类中)编译为同一目录中的 Read.class
。
import java.util.Scanner;
class Read {
public static void main(String[] args) {
String input = "";
Scanner sc = new Scanner(System.in);
input += sc.nextLine() + " ";
input += sc.nextLine() + " ";
System.out.println(input);
}
}
javac read.java
英文:
Here’s an example where the standard input (i.e. System.in
) is piped from another application:
echo 'one line only' | java Read
Or read from a file:
java Read <file_with_one_line.txt
Or using interactive user input:
java Read
<pre><kbd>I am entering one line</kbd>
<kbd>Ctrl</kbd>+<kbd>D</kbd></pre>
These examples assume that you’ve compiled the code you’ve posted (wrapped into a class Read
) to Read.class
in the same directory.
import java.util.Scanner;
class Read {
public static void main(String[] args) {
String input = "";
Scanner sc = new Scanner(System.in);
input += sc.nextLine() + " ";
input += sc.nextLine() + " ";
System.out.println(input);
}
}
javac read.java
答案2
得分: 0
我认为在读取文件时可能会出现这种情况:
我创建了一个新的没有任何内容的 txt 文件(test.txt),并运行了以下代码:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("test.txt");
Scanner sc = new Scanner(file);
String str = sc.nextLine();
}
}
结果为:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Test.main(Test.java:9)
编辑
它也可能出现在使用 System.in 的情况下,因为当关闭一个 Scanner 时,似乎也会关闭 System.in。
考虑以下代码:
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
sc.nextLine();
sc.close();
sc2.nextLine();
}
}
这会抛出以下异常:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Test.main(Test.java:10)
这是因为关闭了具有对 System.in
访问权限的 sc
。这导致 System.in
不再可访问,因此 sc2
无法正常工作并抛出异常。
英文:
I think this is possible when reading files:
I created a new txt-file without any content (test.txt) in it and ran the following code:
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("test.txt");
Scanner sc = new Scanner(file);
String str = sc.nextLine();
}
}
The result:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Test.main(Test.java:9)
Edit
It can also appear with System.in because when closing one scanner it appears to also close System.in.
Consider the following code:
import java.util.Scanner;
import java.io.FileNotFoundException;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);
sc.nextLine();
sc.close();
sc2.nextLine();
}
}
This throws the following exception:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Test.main(Test.java:10)
This happens because sc
(with access to System.in
) was closed. This causes that System.in
is not accessible anymore and therefore sc2
doesn't work properly and throws an exception.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论