I have a file that will be in this format:
12
1 0 0
0 1 0
1 0 1
where the first line is the count
and after is the boolean array content
For reading the array, i count the first line then initialize the N by N boolean array, and then iterate through each line and initialize the boolean array row by row.
However, my reading function wont work as it keeps saying that the boolean array grid wasn't initialized?
the error says:
Error53, 48) java: variable grid might not have been initialized
static void getN(String fileName) throws FileNotFoundException, IOException { int generation = 0; int gridSize = 0; BufferedReader buffReader = new BufferedReader(new FileReader("/Users/alkaitoob/IdeaProjects/untitled9/src/test")); buffReader.readLine(); while(buffReader.readLine()!=null) { gridSize++; } buffReader.close(); boolean [][] grid = new boolean[gridSize][gridSize]; Scanner in = new Scanner(new File("/Users/alkaitoob/IdeaProjects/untitled9/src/test")); gridSize=in.nextInt(); //in.nextLine(); for(int r= 0 ; r<gridSize;r++) { for(int c= 0;c<gridSize;c++) { if(in.nextInt()==1) { grid[r][c] = true; } else { grid[r][c] = false; } } } for(int i=0; i<gridSize; i++) { for(int j=0; j<gridSize; j++) { System.out.print(" " + grid[i][j]); } System.out.println("\n"); } in.close(); } }