0

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);

        }
    }
shrekti
  • 1
  • 2

2 Answers2

0

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.

Michael P
  • 225
  • 3
  • 11
0

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:

  1. One of the input lines could really be empty.

  2. 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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216