英文:
How to read a String from console? NoSuchElementException: No line found
问题
我必须实现接口*Terminal*,它有两个方法。第一个方法只是一个简单的输出,它可以正常工作。第二个方法的目标是通过控制台从用户那里读取输入并返回一个以空格分隔的数组。
```java
public class GameTerminal implements Terminal {
@Override
// 输出方法
@Override
public String[] readInput() {
Scanner scn = new Scanner(System.in);
String input = scn.nextLine();
return input.trim().split("\\s+");
}
}
这段代码抛出了一个异常:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at student.GameTerminal.readInput(GameTerminal.java:20)
...
我尝试使用scn.next()、scn.hasNext()、scn.hasNextLine()和一个BufferedReader来解决这个问题,但都没有成功。我没有第二个Scanner,也没有使用scn.close()。
这段代码在IntelliJ中使用Gradle运行。
有人有解决方案或想法吗?谢谢!
<details>
<summary>英文:</summary>
I have to implement the interface *Terminal*, which has two methods. The first method is just a simple output and it works fine. The goal of the second method is to read the input from the user via console and return a splitted (whitespaces) array.
public class GameTerminal implements Terminal {
@Override
//output method
@Override
public String[] readInput() {
Scanner scn = new Scanner(System.in);
String input = scn.nextLine();
return input.trim().split("\\s+");
}
}
This code throws a Exception:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at student.GameTerminal.readInput(GameTerminal.java:20)
...
I tried to fix the problem with using *scn.next()*, *scn.hasNext()*, *scn.hasNextLine()* and a BufferedReader, but nothing works. I have no second *Scanner* and i don't use *scn.close()*.
The code runs with gradle in IntelliJ.
Has anabody a solution or idea? Thank you!
</details>
# 答案1
**得分**: 1
```java
我认为你只需要在调用之间使用相同的Scanner来保留状态:
```java
public class GameTerminal implements Terminal {
private Scanner scn = new Scanner(System.in);
...
public String[] readInput() {
String input = scn.nextLine();
return input.trim().split("\\s+");
}
}
一个简单的驱动程序:
public class Example {
public static void main(String[] args) throws Exception {
GameTerminal terminal = new GameTerminal();
String[] tokens = null;
while ((tokens = terminal.readInput()) != null) {
System.out.println(Arrays.toString(tokens));
}
}
}
测试运行:
$(/usr/libexec/java_home -v 11)/bin/java Example.java
some tokens
[some, tokens]
some more tokens
[some, more, tokens]
<details>
<summary>英文:</summary>
I think all you have to do is use the same Scanner between calls to preserve state:
```java
public class GameTerminal implements Terminal {
private Scanner scn = new Scanner(System.in);
...
public String[] readInput() {
String input = scn.nextLine();
return input.trim().split("\\s+");
}
}
A simple driver:
public class Example {
public static void main(String[] args) throws Exception {
GameTerminal terminal = new GameTerminal();
String[] tokens = null;
while ((tokens = terminal.readInput()) != null) {
System.out.println(Arrays.toString(tokens));
}
}
}
Test run:
$(/usr/libexec/java_home -v 11)/bin/java Example.java
some tokens
[some, tokens]
some more tokens
[some, more, tokens]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论