使用scanner.nextLine()

时间:2011-02-17 17:28:18

标签: java java.util.scanner

尝试使用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不能按预期工作? Ex之间的唯一区别。 1和2是Ex。 2有一个while循环和一个if-else结构。我不明白为什么这会影响scanner.nextInt()的行为。

5 个答案:

答案 0 :(得分:120)

我认为你的问题是

int selection = scanner.nextInt();

只读取数字,而不是数字后面的行尾或任何内容。当你宣布

String sentence = scanner.nextLine();

这将读取行上剩余的数字(在我怀疑的数字之后没有任何内容)

尝试放置scanner.nextLine();如果您打算忽略该行的其余部分,则在每个nextInt()之后。

答案 1 :(得分:22)

每次想要读取内容时,不要再添加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);
    }
}

因此,要读取一行输入,您只需要scanner.next()具有相同行为分隔符的下一个{Int,Double,...}

与“nextLine()每次”方法的区别在于后者将接受,作为索引<space>33<space>3<space>whatever,而前者仅接受{ {1}}在一条线上

答案 2 :(得分:10)

这是因为当您输入数字然后按Enter键时,input.nextInt()仅消耗数字,而不是“行尾”。像int,double等原始数据类型不消耗“行尾”,因此“行尾”保留在缓冲区中,当input.next()执行时,它从第一个输入的缓冲区消耗“行尾” 。这就是为什么,你的String sentence = scanner.next()只消耗“行尾”并且不等待从键盘读取。

提示:使用scanner.nextLine()代替scanner.next(),因为scanner.next()无法从键盘读取空格。 (在从键盘提供一些空格后截断字符串,仅在空格之前显示字符串。)

答案 3 :(得分:9)

不要尝试使用nextLine()扫描文本;在使用相同扫描仪的nextInt()之后!它与Java Scanner不兼容,许多Java开发人员选择仅使用另一个Scanner进行整数。如果需要,可以将这些扫描仪调用为scan1和scan2。

答案 4 :(得分:3)

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