0

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:

Notification of the terminals

Also, I tried to input the value one by one in my IDEA and it still notified OutOfboundExceptions

Notification of the IDEA

I have no idea what is going on with that Is anybody have some issues with that?

  • The error is in line 40, in method `isValid` – knittl Apr 30 '22 at 07:03
  • @knittl Yeah I see, the thing is line 40 is to find if any index value in the sudoku matrix is greater than 9 or less than 1, which will return the fault boolean value, still have no idea why it may lead to the out of bound exceptions. – cypeter1993 Apr 30 '22 at 07:10
  • Print/inspect `i`, `j`, `row`, and `column` and you will find out that one of them is `<0` or `>=9`. – knittl Apr 30 '22 at 07:21
  • 1
    You made a mistake in this line: `for (int j = 0; i < 9; j++) {`. That should be `j < 9` and not `i < 9` – Abra Apr 30 '22 at 07:25
  • @knittl yeah, I tried to print the matrix and it cannot print it out and I already fixed it. – cypeter1993 Apr 30 '22 at 07:52
  • @knittl it really helps, thanks! – cypeter1993 Apr 30 '22 at 07:53

0 Answers0