0

I encountered the legendary StackOverflow error at lines 25 (random number) and line 29 (where fillGrid() recursively calls itself). I wish to know why this would happen as I have already provided the random number be incremented by one in case it does not fit legally, and if no number is possible then restart everything. I do not wish to get a Sudoku generator algorithm, as I could have googled that, I wish to get an answer as to why this is wrong.Adding a counter shows the program ran 5923 times before Stack overflowing.

import java.util.Random;

class Sudoku{
private Random random = new Random();
private int grid[][] = new int[9][9];
private int rndm, rowLB, rowUB, columnLB, columnUB;
private int count=0;

private void initialise(){
    for (int i=0; i<9; i++) {
        for (int j=0; j<9; j++) {
            grid[i][j]=0;
        }
    }
}

private void fillGrid(){
    initialise();
    for (int i=0; i<9; i++) {
        for (int j=0; j<9; j++) {
            rndm = random.nextInt(9);
            insert(rndm, i, j);
            if (count==10){
                initialise();
                fillGrid();
            }
        }
    }
}

private void insert(int number, int row, int column){
    if(check(number, row, column)){
        grid[row][column]=number;
    }else if (count<=9){
        count++;
        insert((++number)%9, row, column);
    }else{
        return;
    }
}

private boolean check(int number, int row, int column){
    for (int i=0; i<9; i++) {
        if (grid[i][column] == number || grid[row][i] == number) {
            return false;
        }
    }
    findBox(row, column);
    for (int i=rowLB; i<rowUB; i++) {
        for (int j=columnLB; j<columnUB; j++) {
            if (grid[i][j]==number && !(i==row && j==column)){
                return false;
            }
        }
    }
    return true;
}
private void findBox(int row, int column){
    if (0<=row && row<=2){
        rowLB=0; rowUB=2;
    }else if (3<=row && row<=5){
        rowLB=3; rowUB=5;
    }else{
        rowLB=6; rowUB=8;
    }
    if (0<=column && column<=2){
        columnLB=0; columnUB=2;
    }else if (3<=column && column<=5){
        columnLB=3; columnUB=5;
    }else{
        columnLB=6; columnUB=8;
    }
}
public static void main(String args[]){
    Sudoku ob = new Sudoku();
    ob.fillGrid();
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            System.out.print(ob.grid[i][j]);
        }
        System.out.println();
    }
}
}
Da_Doge
  • 19
  • 1
  • 2
  • you have no terminating condition to stop the recursive call ?!! – Ace McCloud Mar 14 '17 at 16:36
  • any time you use recursion if you dont have a base case , it will cause stack overflow..in this case you need to ensure a case where your count will not be 10 and hence the fillgrid method wont be called further – Ace McCloud Mar 14 '17 at 16:36
  • Possible duplicate of [What is a StackOverflowError?](http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – slim Mar 14 '17 at 16:42
  • 1
    Use a debugger to step through this. Presumably there's a point where you expect `count` to go from `10` to some other value. Stepping through with a debugger will show you where the program does something different to what you expect. – slim Mar 14 '17 at 16:47
  • I now flush count by setting it back to 0 just one line prior to calling insert in fillGrid(), so count only keeps track of number of tries made for one digit. However, I still get StackOverFlow error, now both in insert() method for a few times and then the previous fillGrid() error several hundred times. – Da_Doge Mar 14 '17 at 17:13
  • What have you done to track the logic of this? I don't see any description of a debugger session, there's no tracing output, etc. If nothing else, you can insert code to do a top-level trace of the module flow: the first command of each function is a print statement giving the function's name and arguments; on exit, print a corresponding message and return values / status. Controlling flow with global variables is certainly prone to logic debugging problems, so you need to take *some* countermeasures. – Prune Mar 14 '17 at 19:22
  • Doing what @Prune suggested revealed that the following actions keep repeating : Enter insert, enter check, exit check, exit insert. Sometimes findBox is entered and exited and rarely fillgrid is entered and initialise is entered again. There are random blocks of insert exiting over and over again, for some reason. – Da_Doge Mar 15 '17 at 14:05
  • [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. Supply specific code, specific, reproducible input and output. – Prune Mar 15 '17 at 16:05

0 Answers0