Java.util.Scanner.nextLine() 方法
描述
java.util.Scanner.nextLine() 方法将此扫描器前进到当前行并返回被跳过的输入。 此方法返回当前行的其余部分,不包括末尾的任何行分隔符。 位置设置为下一行的开头。 由于此方法继续搜索输入以查找行分隔符,因此如果不存在行分隔符,它可能会缓冲所有搜索要跳过的行的输入。
声明
以下是 java.util.Scanner.nextLine() 方法的声明
public String nextLine()
参数
NA
返回值
该方法返回被跳过的行
异常
NoSuchElementException − 如果没有找到行
IllegalStateException − 如果此扫描仪已关闭
示例
下面的例子展示了 java.util.Scanner.nextLine() 方法的使用。
package com.tutorialspoint;
import java.util.*;
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! \n 3 + 3.0 = 6.0 true ";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// print the next line
System.out.println("" + scanner.nextLine());
// print the next line again
System.out.println("" + scanner.nextLine());
// close the scanner
scanner.close();
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
Hello World! 3 + 3.0 = 6.0 true

