redpanda12 redpanda12 - 5 months ago 6x
Java Question

Read in (from std in) a list of integers/ints on different lines Java

How can I read in one line of integers at a time before moving on to the next one?

I'm receiving input like below (ignore bullet points):

  • 4 -space- - space- 2 - space- - space- - space- 3

  • -space- 84 -space -space- 6

  • 2 -space- -space- -space- 8 -space- -space- 19

where for each row of integers I want to perform operations on only that row. Then I want to get the next row and perform operations on that one and so on.

I won't know how many spaces there are between numbers and I'm currently using Java's scanner.


There might be better way and one possible way could be reading whole line as a single string and then splitting each on spaces. These will be string though and you have to change it to integer.

Scanner scanner = new Scanner(;
String str = scanner.nextLine();
String[] splitStr = str.split(" ");

for(String s: splitStr)