停止扫描仪读取用户输入 java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10560468/
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-10-31 01:33:50  来源:igfitidea点击:

Stop scanner from reading user input java?

javainputjava.util.scanner

提问by FranXh

I am trying to write this method, which keeps reading from the user, until the word "exit" is inputted. I tried with a break and for loop; it didn't work. I was trying with while, but it does not stop even when the word "exit" is inputted. Any ideas how to fix this? Thanks.

我正在尝试编写此方法,该方法会不断从用户那里读取数据,直到输入“退出”一词。我尝试了 break 和 for 循环;它没有用。我尝试了一段时间,但即使输入“退出”一词,它也不会停止。任何想法如何解决这一问题?谢谢。

public void read(Scanner scanner){

while(3<4){
String in = scanner.nextLine();
Scanner scanner_2 = new Scanner(in);

String name = null;

if(scanner_2.hasNext()){
  //create and add the user to the user container class
  name = scanner_2.next();
  System.out.println(name);
}

if(name == exit)
//stop or break the while loop
}
}

回答by Amir Afghani

name == exitis wrong.

name == exit是错的。

You want

你要

name.equals(exit)

or

或者

name.equals("exit")

depending on whether exit is a variable or a string literal, respectively. In Java, ==means reference equality (e.g Are these two references pointing to the same address in memory), whereas .equalsmeans object equivalence, which is typically overridenin the class by the developer.

分别取决于 exit 是变量还是字符串文字。在 Java 中,==意味着引用相等(例如,这两个引用是否指向内存中的相同地址),而 . equals表示对象等价,通常overriden由开发人员在类中。

回答by Bhavik Ambani

if(name == exit)

is wront thats true.

是错的。

But make sure while comparing two string, when one string is constant e.g. "exit"then make sure that it comes first while comparing.

但是请确保在比较两个字符串时,当一个字符串是常量时,例如,请确保在比较时"exit"它排在第一位。

i.e. it should be compared as

即它应该比较为

if ("exit".equals(name))

Not as below way

不像下面的方式

if (name.equals("exit"))

The main reason for making "exit" as first value is, if name is nullthen it will not fire NullPointerExceptionbut if we place nameas first object for comparing then if name is null then it will fires that NullPointerExceptionexception, so make sure this thing any time in future.

将“exit”设为第一个值的主要原因是,如果 name 是,null则它不会触发,NullPointerException但是如果我们将其name作为第一个对象进行比较,那么如果 name 为 null,则它会触发该NullPointerException异常,因此请随时确保此操作未来。

回答by Lemon Juice

Amirs's answer is 100% correct. In addition, use true inside the while loop and, I think it is better to use else... if statement in this case. More readable, thats why :)

阿米尔斯的回答是 100% 正确的。另外,在while循环中使用true,我认为在这种情况下最好使用else...if语句。更具可读性,这就是为什么:)

回答by Nivas

Assuming String exit = "exit";is declared somewhere ar the class level:

假设String exit = "exit";在类级别的某处声明:

name == exit 

checks whether the object referenced by nameand the object referenced by exitare the same. What you want it whether the value ofthe object referenced by nameand the value ofthe object referenced by exitare the same.

检查被引用的对象name和被引用的对象exit是否相同。你希望它是什么是否被引用的对象name由引用的对象exit是相同的。

You do that by

你这样做

if(name.equals(exit))

That said, there are a lot of things that can be improved in the code. I understand you are probably writing this code to learn java, but still some small changes can make the code more readable.

也就是说,代码中有很多可以改进的地方。我知道您可能正在编写此代码来学习 Java,但仍然有一些小的更改可以使代码更具可读性。

Also the second scanner you are using is not needed at all.

此外,根本不需要您使用的第二台扫描仪。

The following code will do the same thing as your code, but is smaller and more readable.

以下代码将执行与您的代码相同的操作,但更小且更具可读性。

    String name = "";
    while(!name.equals("exit")) {
        if(scanner.hasNext()) {
            //create and add the user to the user container class
            name = scanner.next();
            System.out.println(name);
        }

    }

Actually he code can be further improved as:

实际上他的代码可以进一步改进为:

String name = null;
while(scanner.hasNext() && !(name = scanner.next()).equals("exit")) {
    System.out.println(name);
}

But I think you are learning and this may be a bit too much when you are learning.

但我认为你正在学习,当你学习时,这可能有点太多了。