使用 Scanner.next() 获取文本输入

发布于 2024-07-26 03:18:39 字数 754 浏览 4 评论 0原文

我正在尝试从 Java 6 中的键盘获取文本输入。我是该语言的新手,每当我运行以下代码时,我都会收到此错误:

package test1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
    boolean quit = false;
    while (!quit){
        Scanner keyIn;
        String c = "x";
        while (c != "y" && c != "n") {
            keyIn = new Scanner(System.in);
            c = keyIn.next();
            keyIn.close();
        }
        if (c == "n")
            quit = true;
    }
 }
}


Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at test1.Test.main(Test.java:11)

我是否误用了 next() 方法? 我以为它会等待用户输入,但看起来并没有,并抛出异常,说扫描仪中没有留下任何内容。

I am trying to get text input from the keyboard in Java 6. I am new to the language and whenever i run the following code, I get this error:

package test1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
    boolean quit = false;
    while (!quit){
        Scanner keyIn;
        String c = "x";
        while (c != "y" && c != "n") {
            keyIn = new Scanner(System.in);
            c = keyIn.next();
            keyIn.close();
        }
        if (c == "n")
            quit = true;
    }
 }
}


Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at test1.Test.main(Test.java:11)

Am I mis-using the next() method? I thought it would wait for user input but it looks like it isn't and throwing the exception saying that there is nothing left in the scanner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

带刺的爱情 2024-08-02 03:18:39

异常的原因是您在使用一次扫描仪后调用keyIn.close(),这不仅关闭了Scanner,而且还关闭了System.in。 在下一次迭代中,您将创建一个新的 Scanner,它会立即爆炸,因为 System.in 现在已关闭。 要解决这个问题,您应该做的只是在进入 while 循环之前创建一次扫描仪,并完全跳过 close() 调用,因为您不想关闭System.in

修复该问题后,由于您进行了 ==!= 字符串比较,程序仍然无法运行。 在 Java 中比较字符串时,必须使用 equals() 来比较字符串内容。 当您使用 ==!= 时,您正在比较对象引用,因此这些比较在您的代码中将始终返回 false。 始终使用 equals() 来比较字符串。< /a>

while (!quit){
    Scanner keyIn = new Scanner(System.in);
    String c = "x";
    while (!c.equals("y") && !c.equals("n")) {
        c = keyIn.next();
    }
    if (c.equals("n"))
        quit = true;
}

The reason for the exception is that you are calling keyIn.close() after you use the scanner once, which not only closes the Scanner but also System.in. The very next iteration you create a new Scanner which promptly blows up because System.in is now closed. To fix that, what you should do is only create a scanner once before you enter the while loop, and skip the close() call entirely since you don't want to close System.in.

After fixing that the program still won't work because of the == and != string comparisons you do. When comparing strings in Java you must use equals() to compare the string contents. When you use == and != you are comparing the object references, so these comparisons will always return false in your code. Always use equals() to compare strings.

while (!quit){
    Scanner keyIn = new Scanner(System.in);
    String c = "x";
    while (!c.equals("y") && !c.equals("n")) {
        c = keyIn.next();
    }
    if (c.equals("n"))
        quit = true;
}
一笔一画续写前缘 2024-08-02 03:18:39

要评估字符串,您必须使用 .equals

while(!c.equals("y")) { do stuff...

To evaluate strings you have to use .equals

while(!c.equals("y")) { do stuff...

诗酒趁年少 2024-08-02 03:18:39
  • 在循环之外声明您的扫描仪引用。 您不必每次都创建并关闭它。

  • 使用equals方法比较字符串文本,而不是使用==运算符。

    使用

  • declare your Scanner reference outside your loops. you don't have to create it and close it every time.

  • compare string text with the method equals, not with the operator ==.

暗恋未遂 2024-08-02 03:18:39

尝试使用 nextLine() 并仅查看返回的字符串中的第一个元素。

!= 和 == 仅在针对字符或其他基本类型使用时才有效,并且仅在 C# 中有效。 您将需要使用 .equals 来确保检查是否正确相等。

Try using nextLine() and only looking at the first element in the string that is returned.

The != and == will only work when used against characters or other primitive types, that will only work in c#. You will need to use .equals to ensure you are checking for proper equality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文