扫描仪无法扫描第一个字符串

时间:2014-02-14 06:19:56

标签: java java.util.scanner

我有以下代码

public static void main(String args[]){
    Scanner scanner = new Scanner(System.in);
    int noOfTestCases = scanner.nextInt();
    String[] firstStringsList = new String[noOfTestCases];
    String[] secondStringsList = new String[noOfTestCases];
    for(int i=0;i<noOfTestCases;i++){
        firstStringsList[i] = scanner.nextLine();
        secondStringsList[i] = scanner.nextLine();
    }
    for(int i=0;i<noOfTestCases;i++){
        System.out.println("First String : " + firstStringsList[i]);
        System.out.println("Second String : " + secondStringsList[i]);
    }
}

,输出为

1
asd
First String : 
Second String : asd

即我先输入1表示我有1个测试用例涉及获取two String inputs(per test case).但是我只能输入1个分配给secondStringsList[i]的字符串。我正在使用Intellij IDEA。为什么会这样?解决办法:

1 个答案:

答案 0 :(得分:0)

我可能已修复您的问题

public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int noOfTestCases = scanner.nextInt();
        String[] firstStringsList = new String[noOfTestCases];
        String[] secondStringsList = new String[noOfTestCases];

        scanner.nextLine();


        for (int i = 0; i < noOfTestCases; i++) {
            firstStringsList[i] = scanner.nextLine();
            secondStringsList[i] = scanner.nextLine();
        }
        for (int i = 0; i < noOfTestCases; i++) {
            System.out.println("First String : " + firstStringsList[i]);
            System.out.println("Second String : " + secondStringsList[i]);
        }
    }