java.util.NoSuchElementException: 无法从Java Scanner找到行

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

java.util.NoSuchElementException: no Line found from the Java Scanner

问题

我在为您翻译代码的相关部分,以下是翻译好的内容:

public void setUsername(Scanner in) {
    String user = "";

    do {
        while (!in.hasNextLine() && !user.isEmpty() && user.length() < 16 ) {
            Quiz.printError();
            in.next();
        }
        user = in.nextLine(); //Line 33
        username = user;
        Quiz.setProperties("playerusername", username);

    }while(!username.equals(user));
}

public void printPlayerEditor(Scanner in) {
    System.out.println("<<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>");
    System.out.println("欢迎使用玩家编辑器:");
    System.out.println("您当前的用户名是 | " + player.getUsername() + " |");
    System.out.println("请输入一个新的用户名,长度在1-16个字符之间:");
    System.out.println("<<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>");

    player.setUsername(in);

    System.out.println("您的用户名现在是 | " + player.getUsername() + " |\n");
}

请注意,我已将代码中的 HTML 实体字符(如 &quot;)替换为普通的引号字符。

希望这可以帮助您解决代码中的问题。如果您有其他疑问,请随时提出。

英文:

I am creating a quiz game which has one Player instance that the user can create once and then edit the username in Java. My issue comes with my use of the Scanner (I'm pretty sure) which for some reason does not find a new line to read from. My code gives me the following error:

Exception in thread &quot;main&quot; java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at Player.setUsername(Player.java:33)
        at Quiz.printPlayerEditor(Quiz.java:118)
        at Run.main(Run.java:20)

Here is the method setUsername within Player.java that gives error, this is then used in another method printPlayerEditor from Quiz.java, which is then used in the main (Run.java):

public void setUsername(Scanner in) {
    String user = &quot;&quot;;

    do {
        while (!in.hasNextLine() &amp;&amp; !user.isEmpty() &amp;&amp; user.length() &lt; 16 ) {
            Quiz.printError();
            in.next();
        }
        user = in.nextLine(); //Line 33
        username = user;
        Quiz.setProperties(&quot;playerusername&quot;, username);

    }while(!username.equals(user));
}

The Quiz.java code that gives error (which uses the previous method)

public void printPlayerEditor(Scanner in) {
    System.out.println(&quot;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&quot;);
    System.out.println(&quot;Welcome to the Player editor:&quot;);
    System.out.println(&quot;Your current username is | &quot; + player.getUsername() + &quot; |&quot;);
    System.out.println(&quot;Please enter a new username between 1-16 characters:&quot;);
    System.out.println(&quot;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&lt;&gt;&quot;);

    player.setUsername(in);

    System.out.println(&quot;Your username is now | &quot; + player.getUsername() + &quot; |\n&quot;);
}

And then lastly this is used in the main in a switch case that deals with the input. This is where the Scanner comes from:

import java.util.Scanner;

public class Run {
    public static void main(String[] args) {        
        Quiz quiz = new Quiz();
        Player player = new Player();

        quiz.setPlayer(player);
        quiz.populateQuestions();
        int input = quiz.printMenu1();

        Scanner in = new Scanner(System.in);

        switch (input) {                
            case 1:
                if (Quiz.getProperties(&quot;playerusername&quot;).isEmpty()) {
                    quiz.printPlayerCreator(in);
                    input = 2;
                } else {
                    quiz.printPlayerEditor(in);
                    input = 2;
                }
                
            case 2: 
                try {
                    quiz.printUnitMenu(in);

                } catch (NullPointerException e) {
                    quiz.printPlayerCreator(in);
                
                }
                break;

            default:
                Quiz.printError();
                quiz.printMenu1();
                break;
        }

        in.close();
        
    }
}

I've tried passing the same scanner into all of them, and I've tried making the classes static and stuff, but I'm still quite new to Java and cannot get my head around why this error may be occurring.

答案1

得分: 0

不要关闭你不负责的资源。你不需要关闭 System.in(事实上,除非你真的知道这意味着什么,而且确定你要这样做,否则不应关闭它),以及'包装器'资源(比如 Scanner,通常是通过 new ResourceThing(refToSomeOtherResource) 创建的),如果要关闭它们,就关闭你包装的那个东西。换句话说 - 不要这样做。

一些代码检查工具会告诉你应该关闭一个资源。这些检查工具是不正确的(可以理解,追踪资源包装的过滤器有点复杂)。

英文:

Do not ever close resources that you aren't responsible for. You are not responsible for System.in (in fact, you should never close that unless you really know what that means and are sure you want to do that), and 'wrapper' resources (Such as Scanner, generally created with new ResourceThing(refToSomeOtherResource)), if you close them, close the thing you wrapped. In other words - don't.

Some linting tools tell you that you should be closing a resource. Those linting tools are incorrect. (somewhat understandably, keeping track of filters 'through' the resource they wrap is a bit complex).

答案2

得分: 0

是的,hasNextLine 方法不是你要找的。

只需在 nextLine 上循环,并检查是否符合要求。

public void setUsername(Scanner in) {
    String user;
    boolean exit = false;
    do {
        user = in.nextLine();
        if (!user.isEmpty() && user.length() < 16) exit = true;
        else Quiz.printError();
    } while (!exit);
    username = user;
    Quiz.setProperties("playerusername", username);
}
英文:

> "... My issue comes with my use of the Scanner (I'm pretty sure) which for some reason does not find a new line to read from. ..."

Yes, the hasNextLine method isn't what you're looking for.

Just loop on nextLine, and check whether it meets the requirements.

public void setUsername(Scanner in) {
    String user;
    boolean exit = false;
    do {
        user = in.nextLine();
        if (!user.isEmpty() &amp;&amp; user.length() &lt; 16) exit = true;
        else Quiz.printError();
    } while (!exit);
    username = user;
    Quiz.setProperties(&quot;playerusername&quot;, username);
}

huangapple
  • 本文由 发表于 2023年7月20日 15:57:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727787.html
匿名

发表评论

匿名网友

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

确定