-4
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.

braX
  • 11,506
  • 5
  • 20
  • 33
  • 2
    Could you share the error message you get? – Filburt Oct 26 '16 at 07:11
  • 1
    please post the compete stack trace or error message – Timothy Truckle Oct 26 '16 at 07:12
  • ... and please format your code properly. – Turing85 Oct 26 '16 at 07:15
  • even pros make silly mistakes, that is why we are here, but do read our [how to ask guide](http://stackoverflow.com/help/how-to-ask) that makes it easier for us to help you. – dubes Oct 26 '16 at 07:20
  • 2
    The exception tells you your error. You want to get `int` which has a limit of `2,147,483,647` and if you try to pass any number greater than this java will consider it as string. you have tried to pass `34,673,754,745`. This is far greater than maximum limit of `int`. – Blip Oct 26 '16 at 08:00

2 Answers2

0

From the inputs that you have provided to the program, I understand that you have not written the the code that you have posted above. This is because you do not understand the basic requirement of the program.

The program first enters all the clues that are given in the Sudoko. For that you have to enter any integer from 1 to 9 for each cell of the grid for which hint is given and enter 0 for the cells which are blank.

Now you have entered 21213, 123532653, 35426, 124, 437, 54723, 362, 537, 643643436, 34673754745 which are all illegal entries. As the entries should be 0 to 9 and all the inputs provided are more than 9.

Now coming to your error. the line input.nextInt() tries to read from the console the next int that the user has entered. But you have entered a long value instead of int as the last Input. So, since the value is greater than the maximum integer i.e. 2147483647 the Scanner class considers it as String and not an Integer. So, you get the run-time exception.

Blip
  • 3,061
  • 5
  • 22
  • 50
-1

You must and should end the package statement with semi colon(;)

GSK G
  • 1
  • 2