Scanner nextLine() Method – 扫描器 nextLine() 方法

最后修改: 2019年 11月 2日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll briefly look at the nextLine() method of java.util.Scanner class, of course with a focus of learning how to use it in practice.

在这个快速教程中,我们将简要介绍java.util.Scanner类的nextLine()方法,当然,重点是学习如何在实践中使用它。

2. Scanner.nextLine()

2.Scanner.nextLine()

The nextLine() method of the java.util.Scanner class scans from the current position until it finds a line separator delimiter. The method returns the String from the current position to the end of the line.

java.util.Scanner类的nextLine()方法从当前位置开始扫描,直到找到一个行的分隔符。该方法返回从当前位置到行尾的String

Consequently, after the operation, the position of the scanner is set to the beginning of the next line that follows the delimiter.

因此,在操作之后,扫描仪的位置被设置为分界符之后的下一行的开始。

The method will search through the input data looking for a line separator. It may scan all of the input data searching for the line to skip if no line separators are present.

该方法将在输入数据中寻找一个行的分隔符。如果没有行分隔符,它可能会扫描所有的输入数据,寻找要跳过的那一行。

The signature of the nextLine() method is:

nextLine()方法的签名是。

public String nextLine()

The method takes no parameters. It returns the current line, excluding any line separator at the end.

该方法不需要参数。它返回当前行,不包括行尾的任何分隔符。

Let’s look at its usage:

我们来看看它的用法。

try (Scanner scanner = new Scanner("Scanner\nTest\n")) {
    assertEquals("Scanner", scanner.nextLine());
    assertEquals("Test", scanner.nextLine());
}

As we have seen, the method returns the input from the current scanner position until the line separator is found:

正如我们所看到的,该方法返回从当前扫描器位置开始的输入,直到找到行的分隔符。

try (Scanner scanner = new Scanner("Scanner\n")) {
    scanner.useDelimiter("");
    scanner.next();
    assertEquals("canner", scanner.nextLine());
}

In the above example, the call to next() returns ‘S’ and advances the scanner position to point to ‘c’.

在上面的例子中,对next()的调用返回‘S’,并将扫描仪的位置推进到‘c’

Therefore, when we call nextLine() method it returns the input from the current scanner position until it finds a line separator.

因此,当我们调用nextLine()方法时,它会返回从当前扫描器位置开始的输入,直到找到一个行分隔符。

The nextLine() method throws two types of checked exceptions.

nextLine()方法抛出两种类型的检查异常。

Firstly, when no line separator is found, it throws NoSuchElementException:

首先,当没有找到行分隔符时,它会抛出NoSuchElementException

@Test(expected = NoSuchElementException.class)
public void whenReadingLines_thenThrowNoSuchElementException() {
    try (Scanner scanner = new Scanner("")) {
        scanner.nextLine();
    }
}

Secondly, it throws IllegalStateException if the scanner is closed:

其次,如果扫描器被关闭,它会抛出IllegalStateException

@Test(expected = IllegalStateException.class)
public void whenReadingLines_thenThrowIllegalStateException() {
    Scanner scanner = new Scanner("");
    scanner.close();
    scanner.nextLine();
}

3. Conclusion

3.总结

In this to-the-point article, we looked at the nextLine() method of Java’s Scanner class.

在这篇开门见山的文章中,我们研究了Java的Scanner类的nextLine()方法。

Furthermore, we looked at its usage in a simple Java program. Finally, we looked at the exceptions that are thrown by the method and sample code illustrating it.

此外,我们还看了它在一个简单的Java程序中的用法。最后,我们看了该方法抛出的异常和说明该方法的示例代码。

As always, the full source code of the working examples is available over on GitHub.

一如既往,工作实例的完整源代码可在GitHub上获得over