Java Scanner nextLine() 方法



描述

java Scanner nextLine() 方法将此扫描器推进到当前行的下一行,并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何换行符。位置设置为下一行的开头。由于此方法继续搜索输入以查找换行符,如果不存在换行符,它可能会缓冲所有输入以搜索要跳过的行。

声明

以下是java.util.Scanner.nextLine() 方法的声明

public String nextLine()

参数

返回值

此方法返回被跳过的行

异常

  • NoSuchElementException − 如果未找到行

  • IllegalStateException − 如果此扫描器已关闭

基于字符串的 Scanner 获取下一行的示例

以下示例演示了 Java Scanner nextLine() 方法的用法,以将扫描器推进到当前行的下一行。我们使用给定的字符串创建了一个扫描器对象。然后我们使用 nextLine() 方法打印一行,然后使用 hasNextLine() 检查是否还有更多数据。一旦行结束,hasNextLine() 返回 false。最后,使用 close() 方法关闭扫描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      String s = "Hello World! \n 3 + 3.0 = 6 ";

      // create a new scanner with the specified String Object
      Scanner scanner = new Scanner(s);

      // print the next line
      System.out.println(scanner.nextLine());

      // check if there is a next line again
      System.out.println(scanner.hasNextLine());

      // print the next line
      System.out.println(scanner.nextLine());

      // check if there is a next line again
      System.out.println(scanner.hasNextLine());

      // close the scanner
      scanner.close();
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:

Hello World! 
true
 3 + 3.0 = 6 
false

基于用户输入的 Scanner 获取下一行的示例

以下示例演示了 Java Scanner hasNextLine() 方法的用法,以将扫描器推进到当前行的下一行。我们使用 System.in 创建了一个扫描器对象。然后我们使用 nextLine() 方法打印一行,然后使用 hasNextLine() 检查是否还有更多数据。一旦行结束,hasNextLine() 返回 false。最后,使用 close() 方法关闭扫描器。

package com.tutorialspoint;

import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) {

      // create a new scanner with the System Input
      Scanner scanner = new Scanner(System.in);

      // print the next line
      System.out.println(scanner.nextLine());

      // check if there is a next line again
      System.out.println(scanner.hasNextLine());

      // close the scanner
      scanner.close();
   }
}

输出

让我们编译并运行上面的程序,这将产生以下结果:(我们输入 Hello World 并按 Enter 键,然后输入 Bye 并按 Enter 键。)

Hello World
Hello World
Bye
true

基于属性文件的 Scanner 获取下一行的示例

以下示例演示了 Java Scanner hasNextLine() 方法的用法,以将扫描器推进到当前行的下一行。我们使用文件 properties.txt 创建了一个扫描器对象。然后我们使用 hasNextLine() 方法检查每一行,并使用 nextLine() 方法打印它们。最后,使用 close() 方法关闭扫描器。

package com.tutorialspoint;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerDemo {
   public static void main(String[] args) throws FileNotFoundException {

      // create a new scanner with a file as input
      Scanner scanner = new Scanner(new File("properties.txt"));

      // print the next line
      System.out.println(scanner.nextLine());

      // check if there is a next line again
      System.out.println(scanner.hasNextLine());

      // close the scanner
      scanner.close();
   }
}

假设我们在 CLASSPATH 中有一个名为properties.txt 的文件,其内容如下。此文件将用作我们示例程序的输入:

Hello World! 3 + 3.0 = 6

输出

让我们编译并运行上面的程序,这将产生以下结果:

Hello World! 3 + 3.0 = 6
false
java_util_scanner.htm
广告
© . All rights reserved.