简体   繁体   English

Java扫描仪-无法读取用户输入

[英]Java scanner - can't read user input

I want to read user input like: 11 12 13 14 15 16 我想读取用户输入,例如:11 12 13 14 15 16

Scanner sc = new Scanner(System.in);
    while(sc.hasNext()){
        System.out.println(sc.next());

    }
    System.out.println("Test");

but it newer goes out of while loop and prints "Test". 但较新的版本会退出while循环并显示“测试”。 How could i read that input? 我如何阅读该输入?

The method hasNext() works like this: 方法hasNext()工作方式如下:

  • If it sees the end of the file, it returns false; 如果看到文件结尾,则返回false;否则,返回false。
  • If it sees another valid, non-whitespace input, it returns true; 如果看到另一个有效的非空白输入,则返回true;否则,返回true。
  • If neither of the above is true, it waits for the next input the user is going to enter, and doesn't return until he does. 如果以上两个都不成立,它将等待用户要输入的下一个输入,并且直到用户输入后才返回。

Usually, if you use Scanner for files, such a loop will work correctly, because a file has a definite end, and it usually doesn't get stuck waiting for more input. 通常,如果您对文件使用Scanner ,则这样的循环将正常工作,因为文件的末尾有一定的确定性,并且通常不会卡住等待更多的输入。

But when you are working with console input ( System.in , not redirected), then usually the user does not send the end-of-file signal. 但是,当您使用控制台输入( System.in ,未重定向)时,通常用户不会发送文件结束信号。 He just presses Return , and so, hasNext() sits and waits to see if the user will enter more input on the next line and so on. 他只是按Return键 ,所以hasNext()坐了hasNext() ,等待用户是否在下一行输入更多输入,依此类推。

There are two general ways to deal with this: 有两种一般方法可以解决此问题:

  • The user has to actually terminate the input. 用户必须实际终止输入。 After you finish entering all your numbers and press Return , you also need to send the end-of-file sequence, which is usually ctrl D or ctrl Z . 输入完所有数字并按Return键后,还需要发送文件结束序列,通常是ctrl Dctrl Z。

    If you do that, you will not be able to enter any more input to that program. 如果这样做,将无法再对该程序输入任何输入。

  • The program tells the user to enter some particular value that will tell it that the input is over. 该程序告诉用户输入一些特定的值,该值将告诉用户输入已结束。 For example, the string "DONE". 例如,字符串“ DONE”。 When you do that, you have to change the loop to something like: 执行此操作时,必须将循环更改为:

     String nextInput; while( sc.hasNext() && ! (nextInput = sc.next()).equals( "DONE" ) ){ System.out.println(nextInput); } 

You can break the loop depending whether you want to quit or not Eg 您可以根据是否要退出来中断循环,例如

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String next = sc.next();

            if (next.equals("q")) { //if user press q then break the loop
                break;
            }
            System.out.println(next);

        }
        System.out.println("Test");
    }

Use api like: 像这样使用api:

while(sc.hasNextInt()){
    int aba= sc.nextInt();
    if (aba == 0) {//or even non numeric value here would let this loop exit
        break;
    }
}

So you need to enter 0 or even in other way enter non numeric value inorder to come out of loop. 因此,您需要输入0或什至以其他方式输入非数字值才能退出循环。 nextLine method will read whole line just once and then you will need to parse it and then convert to integer so it's good to use sc.nextInt which will do the work for you. nextLine方法将只读取整行,然后您需要对其进行解析,然后转换为整数,因此最好使用sc.nextInt来为您完成工作。

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

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