import java.util.Scanner;
public class Sudoku {
public static void main(String[] args) {
int[][] grid = readAPuzzle();
if (!isValid(grid))
System.out.println("Invalid Input");
else if (search(grid)) {
System.out.println("The solution is found: ");
printGrid(grid);
} else
System.out.println("No solution");
}
public static int[][] readAPuzzle() {
Scanner input = new Scanner(System.in);
System.out.println("Enter a Soduku puzzle:");
int[][] grid = new int[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
grid[i][j] = input.nextInt();
return grid;
}
public static int[][] getFreeCellList(int[][] grid) {
int numberOfFreeCells = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] == 0)
numberOfFreeCells++;
int[][] freeCellList = new int[numberOfFreeCells][2];
int count = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] == 0) {
freeCellList[count][0] = i;
freeCellList[count++][1] = j;
}
return freeCellList;
}
public static void printGrid(int[][] grid) {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
System.out.print((grid[i][j] + " "));
System.out.println();
}
public static boolean search(int[][] grid) {
int[][] freeCellList = getFreeCellList(grid);
int k = 0;
boolean found = false;
while (!found) {
int i = freeCellList[k][0];
int j = freeCellList[k][1];
if (grid[i][j] == 0)
grid[i][j] = 1;
if (isValid(i, j, grid)) {
if (k + 1 == freeCellList.length) {
found = true;
} else {
k++;
}
} else if (grid[i][j] < 9) {
grid[i][j] = grid[i][j] + 1;
} else {
while (grid[i][j] == 9); {
grid[i][j] = 0;
if (k == 0) {
return false;
}
k--;
i = freeCellList[k][0];
j = freeCellList[k][1];
}
grid[i][j] = grid[i][j] + 1;
}
}
return true;
}
public static boolean isValid(int i, int j, int[][] grid) {
for (int column = 0; column < 9; column++)
if (column != j && grid[i][column] == grid[i][j])
return false;
for (int row = 0; row < 0; row++)
if (row != i && grid[row][j] == grid[i][j])
return false;
for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row][col] == grid[i][j])
return false;
return true;
}
public static boolean isValid(int[][] grid) {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
if (grid[i][j] != 0 && !isValid(i, j, grid)) return false;
return true;
}
}
This is my code. I don't know what's wrong with it. I'm trying to let a user enter a Soduku puzzle, and have all spaces that are empty (represented by 0), filled with the correct answer. I seem to have a runtime error that won't let the code execute, but I don't know what it is. Soduku sucks... Also, I'm still a highschool student, so just keep that in mind when using any terms that I probably won't understand. I want to include a picture, but it isn't letting me. It says the entity is too large but that's a problem for another day.
Enter a Soduku puzzle:
21213
123532653
35426
124
437
54723
362
537
643643436
34673754745
Exception in thread "main" java.util.InputMismatchException:
For input string: "34673754745"
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Sudoku.readAPuzzle(Sudoku.java:22)
at Sudoku.main(Sudoku.java:6)
is what appears. I can enter letters, and numbers without an error, but when i exceed 9, it crashes. Also, I seem to be able to enter as many lines of inputs as I want. It doesn't know when to actually execute, I guess.