System.out.println("enter maze");
char[][] maze = new char[height][width];
for (int i=0;i<height;i++){
String line= scanner.nextLine();
for (int j=0;j<width;j++){
maze[i][j]= line.charAt(j);
}
}
I believe it is from the line maze[i][j]= line.charAt(j);
because the input from scanner
is not guaranteed to be of length j
.
The exception is being thrown by line.charAt(j)
, and it is telling you that it cannot get the first character (at position 0) of line
... which means that line
is empty.
Reading your code, it is making the assumption that the input consists of (at least) height
lines each of which contains (at least) width
characters.
I can think of two possible root causes for the exception:
One of the input lines could really be empty.
You could be getting an empty line because of how you handled the Scanner
before this code. For example, if you previously called nextInt
to get read the values of height
and width
, the line separator after those numbers would be left in the scanner's input. So the next call to nextLine
would deliver the rest of that line ... which is probably an empty string.