How to split up input from System.in
Posted
by
zzaw
on Stack Overflow
See other posts from Stack Overflow
or by zzaw
Published on 2012-10-15T03:06:54Z
Indexed on
2012/10/15
3:37 UTC
Read the original article
Hit count: 86
java
Alright so I'm working on something where I take input from System.in; the first line is an int (n) representing the size of a matrix. The next n lines are the matrix itself like so:
10
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 1 0 1 0 0 0 0
0 0 0 0 1 0 0 1 1 0
0 1 0 0 0 0 0 1 0 0
0 0 0 0 0 1 1 0 0 0
1 1 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
The problem is there may be multiple matrix's in a single input, so the next line would have another int and the corresponding matrix underneath until it hits a line with a single 0. I then have to pass each matrix along with the size at the top as a BufferedReader to a method which adds the numbers to a 2D array.
I'm just a little unsure on how to split the input up and send it to the method. Would making a new BufferedReader using skip() and specifying a size each time work? The biggest problem I seem to be running into is reading the size but then the size being excluded as it has already been read.
Cheers
© Stack Overflow or respective owner