Hi all -
I need to read a text file using a scanner class that has the following format:
5 5
5 5 5 5 5
0 0 0 0 5
5 5 0 5 5
5 0 0 0 5
5 5 5 0 0
2 0
The first line is to be read in and used to determine the size of the 2D array.
The first line determines the size of the maze as well. The program is than to skip the first line, and read in the next 5 lines into a 2D array. It is to skip the last line, as those ar the starting coordinates for the ma
I keep getting an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at programfour.ArrayReader.main(ArrayReader.java:28) error. Any advice?
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class ArrayReader {
public static void main(String[] args) throws FileNotFoundException{
File inputFile = new File("maze1.txt");
Scanner sc = new Scanner(inputFile);
int i = 0;
int j = 0;
int count = 1;
int arrayRowSize = sc.nextInt();
int arrayColumnSize = sc.nextInt();
int[][] mazeArray = new int[arrayRowSize][arrayColumnSize];
sc.nextLine();
while(sc.hasNextInt()){
if(count % arrayRowSize == 0){
mazeArray[i][j] = sc.nextInt();
count++;
i++;
j = 0;
} else {
mazeArray[i][j++] = sc.nextInt();
count++;
}
}
}
}