I’m trying to put X’s and spaces into a 2D array and I keep having problem after problem. All I want is the text representation to be in 2D array form but when I debug I notice that some cells have full lines in them instead of a string of one character!
If you can visualize the 2D array as a maze, X’s being the wall and spaces being the open areas to traverse, then it will help you understand what I am doing. I just need each cell to have the proper value: an X or a ” “.
while(scan.hasNextLine() && r < rows) {
while(scan.hasNextLine() && c < columns) {
maze[r][c]=scan.next();
c++;
}
c = 0;
r++;
}
You could try a slightly different approach:
Edit: looking at your code, I do not know if it would cause the problem you stated, but your second while loop has
scan.hasNextLine()and since you are actually usingscan.next()there, it should bescan.hasNext(). Also since you are potentially ending the loop early if there are any columns left in the line, maybe it is not going to the next line.