为什么扫描仪在使用其他下一个功能后会跳过 nextLine()?
Java.util.Scanner 类的 nextLine() 方法将此扫描器前进到当前行并返回被跳过的输入。此函数打印当前行的其余部分,在末尾省略行分隔符。下一个设置在行分隔符之后。由于此方法继续搜索输入以查找行分隔符,因此如果不存在行分隔符,它可能会搜索所有输入以查找要跳过的行。
句法:
public String nextLine()
nextLine() 方法有什么问题?
考虑以下代码示例:
Java
// Java program to show the issue with
// nextLine() method of Scanner Class
import java.util.Scanner;
public class ScannerDemo1 {
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// Taking input
String name = sc.nextLine();
char gender = sc.next().charAt(0);
int age = sc.nextInt();
String fatherName = sc.nextLine();
String motherName = sc.nextLine();
// Print the values to check
// if the input was correctly obtained.
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
System.out.println("Father's Name: "
+ fatherName);
System.out.println("Mother's Name: "
+ motherName);
}
}
Java
// Java program to solve the issue with
// nextLine() method of Scanner Class
import java.util.Scanner;
import java.io.*;
import java.lang.*;
class ScannerDemo1 {
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// Taking input
String name = sc.nextLine();
char gender = sc.next().charAt(0);
// Consuming the leftover new line
// using the nextLine() method
sc.nextLine();
// reading the complete line for the integer
// and converting it to an integer
int age = Integer.parseInt(sc.nextLine());
String fatherName = sc.nextLine();
String motherName = sc.nextLine();
// Print the values to check
// if the input was correctly obtained.
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
System.out.println("Father's Name: "
+ fatherName);
System.out.println("Mother's Name: "
+ motherName);
}
}
- 当此代码针对输入运行时:
abc
m
1
xyz
pqr
- 预期输出:
Name: abc
Gender: m
Age: 1
Father's Name: xyz
Mother's Name: pqr
- 实际输出:
Name: abc
Gender: m
Age: 1
Father's Name:
Mother's Name: xyz
如您所见,nextLine() 方法跳过了要读取的输入,并以母亲的名字代替父亲。因此,预期输出与实际输出不匹配。这个错误很常见,会导致很多问题。
为什么会出现这个问题?
出现此问题的原因是,当使用 Scanner 类的 nextInt() 方法读取人的年龄时,它按预期将值 1 返回给变量 age。但是光标在读取 1 之后仍然在它之后。
abc
m
1_ // Cursor is here
xyz
pqr
所以当使用 Scanner 类的 nextLine() 方法读取父亲的名字时,该方法从光标的当前位置开始读取。在这种情况下,它将在 1 之后开始读取。因此 1 之后的下一行只是一个新行,由 '\n'字符表示。因此,父亲的名字只是'\n'。
如何解决这个问题?
此问题可以通过以下两种方式之一解决:
1. 读取整数的完整行并将其转换为整数,或
句法:
// Read the complete line as String
// and convert it to integer
int var = Integer.parseInt(sc.nextLine());
虽然这种方法不适用于字节字符后的输入字符串(Byte.parseByte(sc.nextLine())。第二种方法适用于这种情况。
2. 通过使用 nextLine() 方法消耗剩余的新行。
句法:
// Read the integer
int var = sc.nextInt();
// Read the leftover new line
sc.nextLine();
下面的示例显示了如何使用 nextLine() 方法解决此问题:
Java
// Java program to solve the issue with
// nextLine() method of Scanner Class
import java.util.Scanner;
import java.io.*;
import java.lang.*;
class ScannerDemo1 {
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// Taking input
String name = sc.nextLine();
char gender = sc.next().charAt(0);
// Consuming the leftover new line
// using the nextLine() method
sc.nextLine();
// reading the complete line for the integer
// and converting it to an integer
int age = Integer.parseInt(sc.nextLine());
String fatherName = sc.nextLine();
String motherName = sc.nextLine();
// Print the values to check
// if the input was correctly obtained.
System.out.println("Name: " + name);
System.out.println("Gender: " + gender);
System.out.println("Age: " + age);
System.out.println("Father's Name: "
+ fatherName);
System.out.println("Mother's Name: "
+ motherName);
}
}
- 当此代码针对输入运行时:
abc
m
1
xyz
pqr
- 预期输出:
Name: abc
Gender: m
Age: 1
Father's Name: xyz
Mother's Name: pqr
- 实际输出:
Name: abc
Gender: m
Age: 1
Father's Name: xyz
Mother's Name: pqr