How to use .nextInt() and hasNextInt() in a while

2019-01-08 01:17发布

So I want my program to read an input, which has some integers in one line, for example:

1 1 2

Then it should read every integer separately and print it in a new line. The number of integers the program has to read is not given in advance, so what I am trying to do is use a while loop, which ends after there are no more integers to read. This is the code I wrote:

while (scan.hasNextInt()) {
    int x = scan.nextInt();
    System.out.println(x);
}

but it's not working correctly, because the loop never ends, it just wants the user to input more integers. What am I missing here?

3条回答
霸刀☆藐视天下
2楼-- · 2019-01-08 01:49

If you like to stop your loop after the line, create your Scanner like this:

public static void main(final String[] args) {
    Scanner scan = new Scanner(System.in).useDelimiter(" *");
    while (scan.hasNextInt() && scan.hasNext()) {
        int x = scan.nextInt();
        System.out.println(x);
    }

}

The trick is to define a delimiter that contains whitespace, the empty expression, but not the next line character. This way the Scanner sees the \n followed by a delimiter (nothing) and the input stops after pressing return.

Example: 1 2 3\n will give the following tokens: Integer(1), Integer(2), Integer(3), Noninteger(\n) Thus the hasNextInt returns false.

查看更多
The star\"
3楼-- · 2019-01-08 01:57

The hasNextInt call blocks until it has enough information to make the decision of "yes/no".

Press Ctrl+Z on Windows (or Ctrl+D on "unix") to close the standard input stream and trigger an EOF. Alternatively, type in a non-integer and press enter.

Console input is normally line-buffered: enter must be pressed (or EOF triggered) and the entire line will be processed at once.

Examples, where ^Z means Ctrl+Z (or Ctrl+D):

1 2 3<enter>4 5 6^Z   -- read in 6 integers and end because stream closed
                      -- (two lines are processed: after <enter>, after ^Z)
1 2 3 foo 4<enter>    -- read in 3 integers and end because non-integer found
                      -- (one line is processed: after <enter>)

See also:

查看更多
地球回转人心会变
4楼-- · 2019-01-08 02:02

Your scanner basically waits until an end of file comes in. And if you use it in the console that does not happen, so it will continue to run. Try reading the integers from a file, you will notice your program will terminate.

In case you are new to reading from a file, create a test.txt in your project folder and use Scanner scan = new Scanner(new File("test.txt")); with your code.

查看更多
登录 后发表回答