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
EDIT: Got it working using Bhesh Gurung's method, thanks a ton. This is what I ended up with. I think some of the if statements are redundant but it works.
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
ArrayList<GraphAdjMatrix> mat = new ArrayList<GraphAdjMatrix>();
try
{
String line = buffer.readLine().trim();
String[] tokens = line.split("\\s+");
boolean[][] adj;
int n = Integer.parseInt(tokens[0]);
while (n != 0) {
if (tokens.length == 1 && n > 0) {
adj = new boolean[n][n];
for (int i = 0; i < n; i++) {
line = buffer.readLine().trim();
tokens = line.split("\\s+");
if (tokens.length != n)
{
throw new Error("bad format: adjacency matrix");
}
for (int j = 0; j < n; j++)
{
int entry = Integer.parseInt(tokens[j]);
adj[i][j] = entry != 0;
}
}
mat.add(new GraphAdjMatrix(adj, n));
}
line = buffer.readLine().trim();
tokens = line.split("\\s+");
n = Integer.parseInt(tokens[0]);
}
}
catch (IOException x) { throw new Error("bad input stream"); }