使用 scanner.nextLine()

我在尝试使用 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 循环和 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就是那个 Ex。2有一个 while 循环和一个 if-else 结构。我不明白为什么这会影响 Scanner.nextInt ()的行为。

697998 次浏览

I think your problem is that

int selection = scanner.nextInt();

reads just the number, not the end of line or anything after the number. When you declare

String sentence = scanner.nextLine();

This reads the remainder of the line with the number on it (with nothing after the number I suspect)

Try placing a scanner.nextLine(); after each nextInt() if you intend to ignore the rest of the line.

Rather than placing an extra scanner.nextLine() each time you want to read something, since it seems you want to accept each input on a new line, you might want to instead changing the delimiter to actually match only newlines (instead of any whitespace, as is the default)

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);
}
}

Thus, to read a line of input, you only need scanner.next() that has the same behavior delimiter-wise of next{Int, Double, ...}

The difference with the "nextLine() every time" approach, is that the latter will accept, as an index also <space>3, 3<space> and 3<space>whatever while the former only accepts 3 on a line on its own

or

int selection = Integer.parseInt(scanner.nextLine());

It's because when you enter a number then press Enter, input.nextInt() consumes only the number, not the "end of line". Primitive data types like int, double etc do not consume "end of line", therefore the "end of line" remains in buffer and When input.next() executes, it consumes the "end of line" from buffer from the first input. That's why, your String sentence = scanner.next() only consumes the "end of line" and does not wait to read from keyboard.

Tip: use scanner.nextLine() instead of scanner.next() because scanner.next() does not read white spaces from the keyboard. (Truncate the string after giving some space from keyboard, only show string before space.)

Don't try to scan text with nextLine(); AFTER using nextInt() with the same scanner! It doesn't work well with Java Scanner, and many Java developers opt to just use another Scanner for integers. You can call these scanners scan1 and scan2 if you want.