什么情景下会使用 nextLine() 引发 NoSuchElement 异常?

huangapple go评论86阅读模式
英文:

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 &#39;one line only&#39; | java Read

Or read from a file:

java Read &lt;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 = &quot;&quot;;
        Scanner sc = new Scanner(System.in);

        input += sc.nextLine() + &quot; &quot;;
        input += sc.nextLine() + &quot; &quot;;

        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(&quot;test.txt&quot;);
		Scanner sc = new Scanner(file);
		String str = sc.nextLine();
	}
}

The result:

Exception in thread &quot;main&quot; 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 &quot;main&quot; 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.

huangapple
  • 本文由 发表于 2020年9月18日 21:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63956406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定