简体   繁体   English

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

[英]Stop scanner from reading user input java?

I am trying to write this method, which keeps reading from the user, until the word "exit" is inputted. 我正在尝试编写这种方法,该方法不断向用户读取,直到输入“exit”字样。 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. 谢谢。

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

name == exit is wrong. name == exit错误。

You want 你要

name.equals(exit)

or 要么

name.equals("exit")

depending on whether exit is a variable or a string literal, respectively. 取决于exit分别是变量还是字符串文字。 In Java, == means reference equality (eg Are these two references pointing to the same address in memory), whereas . 在Java中, ==表示引用相等(例如,这两个引用是否指向内存中的相同地址),而。 equals means object equivalence, which is typically overriden in the class by the developer. equals表示对象等价,通常由开发人员在类中overriden

if(name == exit)

is wront thats true. 这是真的。

But make sure while comparing two string, when one string is constant eg "exit" then make sure that it comes first while comparing. 但要确保在比较两个字符串时,当一个字符串是常量时,例如"exit"然后确保它在比较时首先出现。

ie 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 null then it will not fire NullPointerException but if we place name as first object for comparing then if name is null then it will fires that NullPointerException exception, so make sure this thing any time in future. 将“exit”作为第一个值的主要原因是,如果name为null则它不会触发NullPointerException但如果我们将name作为第一个对象进行比较,那么如果name为null则会触发NullPointerException异常,因此请确保将来任何时候的事情。

Amirs's answer is 100% correct. Amirs的回答是100%正确的。 In addition, use true inside the while loop and, I think it is better to use else... if statement in this case. 另外,在while循环中使用true,我认为在这种情况下使用else ... if语句会更好。 More readable, thats why :) 更具可读性,这就是为什么:)

Assuming String exit = "exit"; 假设String exit = "exit"; is declared somewhere ar the class level: 在类级别声明:

name == exit 

checks whether the object referenced by name and the object referenced by exit are the same. 检查name引用的对象和exit引用的对象是否相同。 What you want it whether the value of the object referenced by name and the value of the object referenced by exit are the same. 你希望它是什么是否被引用的对象的值 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. 但我认为你正在学习,当你学习时,这可能有点太多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM