nextLine()java Scanner类的问题

时间:2013-01-16 00:57:17

标签: java

import java.util.Scanner;

public class b {

    public static void main(String[] args){

        Scanner keyboard = new Scanner(System.in);
        String[] lines = new String[5];

        lines[0] = keyboard.nextLine();
        lines[1] = keyboard.nextLine();
        lines[2] = keyboard.nextLine();
        lines[3] = keyboard.nextLine();
        lines[4] = keyboard.nextLine();

        for (int i = 0; i < lines.length; i++) {
            System.out.println(lines[i]);
        }
    }
}

为什么以上代码不允许我存储以下内容:

Hello there
How are you
My name is
Bill Gates
What is yours?

我得到的输出是:

Hello there
How are you
My name is

Process completed.

我知道原语和nextInt()存在问题,但我认为nextLine()应该解决这个问题。

1 个答案:

答案 0 :(得分:1)

当我运行时,我明白了:

Hello there
How are you
My name is
Bill Gates
What is yours?

正如所料。

检查您是否未运行旧版程序。还可以从命令行测试您的应用程序,以排除格式化方面的任何IDE问题。