In a program I am writing for homework, one method yields an unexpected error:
(The variable n has been declared 9 earlier in the program)
private boolean[] getAllowedValues(int row, int col) { boolean[] valNums = new boolean[n]; // Set the initial value of all squares to true for (int i = 0; i < n; i++) valNums[i] = true; // Check row for (int i = 0; i < n; i++) { if (board[row][i] != LOCATION_EMPTY) valNums[board[row][i]] = false; } // Check column for (int i = 0; i < n; i++) { if (board[i][col] != LOCATION_EMPTY) valNums[board[i][col]] = false; } // Check the nine sub-grids int rl = (row / 3) * 3; int rh = (row / 3) * 3 + 3; int cl = (col / 3) * 3; int ch = (col / 3) * 3 + 3; for (int r = rl; r < rh; r++) { for (int c = cl; c < ch; c++) { if (board[r][c] != LOCATION_EMPTY) valNums[board[r][c]] = false; } } System.out.println(valNums); return valNums; }
This is the error I get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Sudoku.SudokuPuzzle.getAllowedValues(SudokuPuzzle. java:232)
at Sudoku.SudokuPuzzle.main(SudokuPuzzle.java:278)
This method is called in the main method as a part of the Sudoku constructor as part of a switch:
Thanks!