Java,Scanner,nextLine()方法错误

时间:2016-01-31 16:08:34

标签: java input

您能告诉我以下代码有什么问题:

    Scanner input = new Scanner(System.in);
    Vector <Vector <String>> allValues = new Vector <Vector <String>>();
    Vector <String> currentTestValues = new Vector <String>();
    int tests = input.nextInt();
    for (int i = 0; i < tests; i++){
        int deposits = input.nextInt();
        for (int j = 0; j < deposits; j++){
            String s = input.nextLine();
            currentTestValues.add(s);
        }
        allValues.add(currentTestValues);
        currentTestValues.clear();
    }
    for (Vector <String> v : allValues){
        for (String s : v){
            System.out.println(s);
        }
    }

似乎在input.nextLine();

之后终止

如何解决?

1 个答案:

答案 0 :(得分:0)

已使用input.nextLine()currentTestValues.clear();。这将从列表中删除您的值,因此从vector中删除它。删除它。

Scanner input = new Scanner(System.in);
Vector <Vector <String>> allValues = new Vector <Vector <String>>();
Vector <String> currentTestValues = new Vector <String>();
int tests = Integer.parseInt(input.nextLine());
for (int i = 0; i < tests; i++){
    int deposits = Integer.parseInt(input.nextLine());
    for (int j = 0; j < deposits; j++){
        String s = input.nextLine();
        currentTestValues.add(s);
    }
    allValues.add(currentTestValues);

}
for (Vector <String> v : allValues){
    for (String s : v){
        System.out.println(s);
    }
}
}
相关问题