Java Scanner nextLine()方法与示例
扫描仪类nextLine()方法
nextLine()方法在java.util包中可用。
nextLine()方法用于获取跳过的行。
nextLine()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
nextLine()方法在返回新行时可能会引发异常。
NoSuchElementException:如果不存在任何行,则可能引发此异常。
IllegalStateException:如果未打开此扫描器,则可能引发此异常。
语法:
public String nextLine();
参数:
它不接受任何参数。
返回值:
该方法的返回类型为String,它返回跳过的行。
示例
//Java程序演示示例
//nextLine()扫描器的字符串方法的说明
import java.util.*;
public class NextLineOfScanner {
public static void main(String[] args) {
String str = "Hi, true \n IncludeHelp! 8 + 2.0f = 10.0f";
//实例化扫描仪
//给定str-
Scanner sc = new Scanner(str);
//显示扫描仪
System.out.println("sc.nextLine(): " + sc.nextLine());
//显示NextLine-
System.out.println("sc.nextLine(): " + sc.nextLine());
//关闭扫描仪
sc.close();
}
}输出结果
sc.nextLine(): Hi, true sc.nextLine(): IncludeHelp! 8 + 2.0f = 10.0f