如何从控制台读取字符串?NoSuchElementException:找不到行

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

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(&quot;\\s+&quot;);
}

}

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&#39;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(&quot;\\s+&quot;);
    }
}

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]

huangapple
  • 本文由 发表于 2020年8月6日 20:04:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63283226.html
匿名

发表评论

匿名网友

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

确定