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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1071965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:07:15  来源:igfitidea点击:

Using Scanner.next() to get text input

javajava.util.scanner

提问by wakingrufus

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:

我正在尝试从 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)

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.

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

采纳答案by John Kugelman

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

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

修复该程序后,由于您执行的==!=字符串比较,该程序仍然无法运行。在 Java 中比较字符串时,您必须使用equals()比较字符串内容。当您使用==!=比较对象引用时,这些比较将始终在您的代码中返回 false。总是equals()用来比较字符串。

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;
}

回答by Corey Sunwold

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

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

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.

!= 和 == 仅在用于字符或其他原始类型时才有效,这仅在 c# 中有效。您将需要使用 .equals 来确保您正在检查正确的相等性。

回答by cd1

  • 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 ==.

  • 在循环之外声明您的 Scanner 引用。您不必每次都创建并关闭它。

  • 将字符串文本与方法进行比较equals,而不是与运算符进行比较==

回答by cd1

To evaluate strings you have to use .equals

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

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

while(!c.equals("y")) { 做事...