Please, I do need some help. I need help with my checking sudoku valid codes. And here's my code:
package Array.MultiDemensionArray;
import java.util.*;
public class CheckSudoku {
public static void main(String[] args) {
int result[][] = readSolution();
System.out.println((isValid(result))? "It is valid":"It is not valid");
}
// Read the solution from the console;
public static int[][] readSolution() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the value of the sudoku: ");
int samplesudo[][] = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
samplesudo[i][j] = input.nextInt();
}
}
return samplesudo;
}
// Check all the items in sudoku matrix.
public static boolean isValid(int[][] matrix) {
for (int i = 0; i < 9; i++) {
for (int j = 0; i < 9; j++) {
if (matrix[i][j] < 1 || matrix[i][j] > 9 ) {
return false;
}
if(!isValid(i,j,matrix)){
return false;
}
}
}
return true;
}
public static boolean isValid(int i, int j, int[][] matrix) {
// Check the validation of the column for the sudoku
for (int column = 0; column < 9; column++) {
if (column != i && matrix[column][j] == matrix[i][j]) {
return false;
}
}
// Check the validation of the row for the sudoku
for (int row = 0; row < 9; row++) {
if (row != j && matrix[i][row] == matrix[i][j]) {
return false;
}
}
// Check the validation of the 3*3 matrix the 9*9 sudoku
for (int column = (i / 3) * 3; column < (i / 3) * 3 + 3; column++) {
for (int row = (j / 3) * 3; row < (i / 3) * 3 + 3; row++) {
if (column != i && row != j && matrix[column][row] == matrix[i][j]) {
return false;
}
}
}
return true;
}
}
I tried to debug in the IDEA and it notifies works normally. Then I tried to load the data from the txt files in the terminal and it notified:
Also, I tried to input the value one by one in my IDEA and it still notified OutOfboundExceptions
I have no idea what is going on with that Is anybody have some issues with that?