使用 Scanner.next() 获取文本输入
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
异常的原因是您在使用一次扫描仪后调用
keyIn.close()
,这不仅关闭了Scanner
,而且还关闭了System.in
。 在下一次迭代中,您将创建一个新的Scanner
,它会立即爆炸,因为System.in
现在已关闭。 要解决这个问题,您应该做的只是在进入while
循环之前创建一次扫描仪,并完全跳过close()
调用,因为您不想关闭System.in
。修复该问题后,由于您进行了
==
和!=
字符串比较,程序仍然无法运行。 在 Java 中比较字符串时,必须使用 equals() 来比较字符串内容。 当您使用==
和!=
时,您正在比较对象引用,因此这些比较在您的代码中将始终返回 false。 始终使用equals()
来比较字符串。< /a>The reason for the exception is that you are calling
keyIn.close()
after you use the scanner once, which not only closes theScanner
but alsoSystem.in
. The very next iteration you create a newScanner
which promptly blows up becauseSystem.in
is now closed. To fix that, what you should do is only create a scanner once before you enter thewhile
loop, and skip theclose()
call entirely since you don't want to closeSystem.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 useequals()
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 useequals()
to compare strings.要评估字符串,您必须使用 .equals
while(!c.equals("y")) { do stuff...
To evaluate strings you have to use .equals
while(!c.equals("y")) { do stuff...
在循环之外声明您的扫描仪引用。 您不必每次都创建并关闭它。
使用
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==
.尝试使用 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.