-1

Can you tell me what is wrong with the following code:

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

It seems to terminate after input.nextLine();

How to fix it?

Roman C
  • 47,329
  • 33
  • 60
  • 147
Poyr23
  • 123
  • 1
  • 8
  • 1
    Terminate ... *how*? – kolossus Jan 31 '16 at 16:10
  • may be it means skipping. – SatyaTNV Jan 31 '16 at 16:11
  • No, I mean the program stopps. For example if I enter: 1 2 asd the program stops. – Poyr23 Jan 31 '16 at 16:12
  • 1
    Please read this link and tell if it solves the problem: http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods – Tunaki Jan 31 '16 at 16:13
  • try String s = input.next(); – Anoop LL Jan 31 '16 at 16:15
  • It's not an error, the program just stops, as if it is done. @Tunaki I tryed it, I put input.nextLine(); after the int deposits = input.nextLine(); . But it is not filling the vector. All I have now is just an input. – Poyr23 Jan 31 '16 at 16:22
  • I don't understand why did people downvote the question. Its a valid question. Did the people who downvote know the answer for the problem ? – thedarkpassenger Jan 31 '16 at 16:25
  • @AnshulJain Yea I can't understand it either... – Poyr23 Jan 31 '16 at 16:27
  • "terminate after input.nextLine();" is not proper problem description. Post example of your input, and expected result. Also explain how what you get is different then what expected in question itself. To do it use [edit] option. – Pshemo Jan 31 '16 at 16:29

1 Answers1

0

Used input.nextLine() and currentTestValues.clear();. This will remove your values from list and so from vector.So remove it.

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);
    }
}
}
Anoop LL
  • 1,488
  • 2
  • 18
  • 29