I'm trying to read in a grid from a text file to construct a graph out of it.
Goal nodes are indicated by a X while other nodes are indicated by a ..
I have the number of rows and the number of columns as well. So, I'm basically just trying to read in the next line for each row, then get the character at each column location by passing the iterator for column location and check it against an X to see if it should be marked as a goal node.
goalGraph = new int[rows][cols];
for (int i = 0; i < rows; i++) {
String readLine = in.nextLine();
System.out.println(readLine);
for (int ii = 0; ii < cols; ii++) {
char c = readLine.charAt(ii);
if (c == 'x') {
goalGraph[i][ii] = 1;
}
else {
goalGraph[i][ii] = 0;
System.out.print(".");
}
}
}
But I keep getting an arrayoutofbounds error at index (0).
Another strange thing is that it is not printing readLine when the lower code is implemented, but taking it out so it looks like:
goalGraph = new int[rows][cols];
for (int i = 0; i < rows; i++) {
String readLine = in.nextLine();
System.out.println(readLine);
/**
for (int ii = 0; ii < cols; ii++) {
char c = readLine.charAt(ii);
if (c == 'x') {
goalGraph[i][ii] = 1;
}
else {
goalGraph[i][ii] = 0;
System.out.print(".");
}
}
**/
}
As a result, the line being read in are printed, and I get the correct string:
X.....
...X..
......
.X....
Can anyone point me in the right direction?
Thanks!
gridthat you are reading from in the text file?