I want to be able to read in a map from a file that looks something like:
0, 0, 0, 0, 0
0, 0, 1, 0, 0
0, 1, 1, 1, 1
0, 1, 1, 1, 0
0, 0, 1, 1, 0
And create an array list that looks like:
{[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 1, 0]}
I have tried using br.readLine() but it appears to be getting stuck but not throwing an error in the middle.
public static int[][] loadFile() throws IOException{
FileReader in = new FileReader(Main.currentFilePath + Main.currentFile); BufferedReader br = new BufferedReader(in); String line; int [] intArray = {}; int [][] fileArray = {}; int j = 0; while ((line = br.readLine()) != null) { List<String> stringList = new ArrayList<String>(Arrays.asList(line.split(","))); String[] stringArray = stringList.toArray(new String[0]); List<Integer> intList = new ArrayList<Integer>(); System.out.println("RRRRR"); for(int i = 0; i < stringList.size(); i++) { Scanner scanner = new Scanner(stringArray[i]); System.out.println("GGGGG"); while (scanner.hasNextInt()) { intList.add(scanner.nextInt()); intArray = intList.parallelStream().mapToInt(Integer::intValue).toArray(); System.out.println("FFFF"); } System.out.println(fileArray[j][i]); fileArray[j][i] = intArray[i]; } j++; } return fileArray; }
"error in the middle..."is???Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0because yourfileArrayhas a length of0, it's impossible for you to add any new rows/columns to it