0

I tried to get inputs via scanner and in the past, I use enter to get to the next set of inputs. For ex.

Input 1 <enter> 
Input 2 <enter>

However this time, it only accepts in the same line , taking spaces as delimiter.

Scanner in = new Scanner(System.in);
int a,b,n,t;
String input_line;
String inputs[]= new String[3];

t = in.nextInt();

in.reset(); //Tried resetting Scanner to see if this works
input_line = in.nextLine();
inputs = input_line.split(" ");

for(String s:inputs)
System.out.println(s);

For instance, I expect to take the variable t in first line and then move on to the second line for input_line scanning. But if I hit enter after entering t, the program ends.

What am I missing here?
(Merging with another question was suggested but , let me explain, the Scanner does not skip any inputs).

Anirudh Ramesh
  • 31
  • 4
  • 11
  • 1
    `nextInt()` does not consume the whole line read some docs – singhakash May 01 '15 at 15:55
  • @singhakash I tried the same code a while back with all nextInt() as inputs, it moved on to other line and I was able to give inputs. – Anirudh Ramesh May 01 '15 at 15:57
  • See my answer to similar question here: http://stackoverflow.com/questions/29008778/java-scanner-execute-only-the-int-and-skip-the-strings-data-types-when-i-inp/29010021#29010021 – Jaroslaw Pawlak May 01 '15 at 16:01
  • @Tom thanks. However, there is a slight difference in my case. It doesn't skip . – Anirudh Ramesh May 01 '15 at 16:01
  • if you want to continue getting inputs you have to set your input_line in a loop at least.... – ola May 01 '15 at 16:02
  • @WhatIfTheyGetMe *"But if I hit enter after entering t, the program ends. "* -> *"the Scanner does not skip any inputs"*. This is a contradiction. The program ends, because _it skips_ the input for `input_line`. – Tom May 01 '15 at 16:16

1 Answers1

0

Without any testing I would think you would need something like this

 Scanner in = new Scanner(System.in);
     int a,b,n,t;
     String input_line;
     String[] input_numbers = new String[3];

    t = in.nextInt();
    in.nextLine();
    input_line = in.nextLine();

    while(!input_line.equals("")){
          input_numbers = input_line.split(" ");
          // do what you want with numbers here for instance parse to make each string variable into int or create new scanner to do so
          input_line = in.nextLine();
    }
}
ola
  • 782
  • 2
  • 10
  • 29