提问人:Taylor P. 提问时间:2/18/2011 最后编辑:Eric LeschinskiTaylor P. 更新时间:12/23/2017 访问量:703968
使用 scanner.nextLine() [duplicate]
Using scanner.nextLine() [duplicate]
问:
我在尝试使用 java.util.Scanner 中的 nextLine() 方法时遇到了问题。
这是我尝试过的:
import java.util.Scanner;
class TestRevised {
public void menu() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
}
示例#1:此示例按预期工作。该行等待输入,然后继续执行 。String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
这将产生输出:
Enter a sentence: Hello.
Enter an index: 0
Your sentence: Hello.
Your index: 0
// Example #2
import java.util.Scanner;
class Test {
public void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu Options\n");
System.out.println("(1) - do this");
System.out.println("(2) - quit");
System.out.print("Please enter your selection:\t");
int selection = scanner.nextInt();
if (selection == 1) {
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
else if (selection == 2) {
break;
}
}
}
}
示例#2:此示例无法按预期工作。此示例使用 while 循环和 and if - else 结构来允许用户选择要执行的操作。一旦程序到达 ,它不会等待输入,而是执行 行 。String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
这将产生输出:
Menu Options
(1) - do this
(2) - quit
Please enter your selection: 1
Enter a sentence: Enter an index:
这使得无法输入句子。
为什么示例 #2 无法按预期工作?例 1 和 例 2 之间的唯一区别是例 2 具有 while 循环和 if-else 结构。我不明白为什么这会影响 scanner.nextInt() 的行为。
答:
我认为你的问题是
int selection = scanner.nextInt();
只读取数字,不读取行尾或数字后面的任何内容。当您声明时
String sentence = scanner.nextLine();
这将读取带有数字的行的其余部分(我怀疑数字后面没有任何东西)
尝试放置 scanner.nextLine();在每个 nextInt() 之后,如果您打算忽略该行的其余部分。
评论
nextInt
与其在每次要读取某些内容时都放置额外的内容,因为您似乎想接受新行上的每个输入,您可能希望更改分隔符以实际仅匹配换行符(而不是默认的任何空格)scanner.nextLine()
import java.util.Scanner;
class ScannerTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.useDelimiter("\\n");
System.out.print("Enter an index: ");
int index = scanner.nextInt();
System.out.print("Enter a sentence: ");
String sentence = scanner.next();
System.out.println("\nYour sentence: " + sentence);
System.out.println("Your index: " + index);
}
}
因此,要读取一行输入,您只需要具有与 next{Int, Double, ...} 相同的行为分隔符scanner.next()
与“nextLine() every time”方法的区别在于,后者将接受,作为索引,而前者只接受一行<space>3
3<space>
3<space>whatever
3
或
int selection = Integer.parseInt(scanner.nextLine());
这是因为当您输入一个数字然后按 Enter 键时,只使用数字,而不是“行尾”。int、double 等原始数据类型不使用“行尾”,因此“行尾”保留在缓冲区中,执行时,它会从第一个输入的缓冲区中使用“行尾”。这就是为什么,您只使用“行尾”,而不是等待从键盘上读取。input.nextInt()
input.next()
String sentence = scanner.next()
提示:使用 instead of because 不会从键盘上读取空格。(在键盘上留出一些空格后截断字符串,只在空格之前显示字符串。scanner.nextLine()
scanner.next()
scanner.next()
不要尝试使用 nextLine() 扫描文本;在将 nextInt() 与相同的扫描仪一起使用之后!它不适用于 Java Scanner,许多 Java 开发人员选择只使用另一个 Scanner 来处理整数。如果需要,可以将这些扫描仪称为scan1和scan2。
下一个:从扫描仪获取 char 输入
评论