This (title) is happening for some reason.
I have a text file like this:
5
H H H H H
V H H H H
H X X X X
H D H H H
H H H H X
I parsed the file with
input.useDelimiter(" ");
I read in and store the number on line 1 in a variable named "size"
Then I proceeded to read the file with a nested loop and stored the elements in a 2D array:
for(int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
maze[x][y] = input.next();
System.out.print(maze[x][y] + " ");
}
}
For some reason, the last element of a line and the first element of the next line is always read together, with a newline in between. So maze[0][4]
stores H\nV
, when it's only supposed to store H
.
I'm clueless about why this is happening.
EDIT: I noticed the problem goes away after I add a space at the end of each line in the text file, but is there a way to fix this issue w/o altering the file?