Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to prepare sudoku, I made my code and everything worked fine but then I had to add interfaces and now I have a problem because once it works and once not. There is a problem with arrayList I guess.

  public class FillSudokuSolver implements SudokuSolver {
    public int solve(int index, SudokuBoard tab){
    boolean check = true;

    while(check){
        fill(index,tab);
        if(tab.getDigit(8,8)!=0){
            check = false;
            return 0;
        }
        else{
            tab.setBoard();
            index=0;
           }
      }
       return 0;
  }



     public int fill(int index, SudokuBoard tab){

     if(index>80)
         return 0;     

     int x = index/9;
     int y = index%9;

    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i <= 9; i++)
        numbers.add(i);        
    Collections.shuffle(numbers);

    if(tab.getDigit(x,y) != 0){
        solve(index + 1,tab);
    }
    else
    {
        int number = tab.getNextPossibleNumber(x, y, numbers);
        if (number == -1)
            return 0;


        tab.setDigit(x,y,number);
        solve(index + 1,tab);  
    }

    return 0;
}


   public int getNextPossibleNumber(int x, int y, List<Integer> numbers){
   while (numbers.size() > 0){
    int number = numbers.remove(0);
    if (isPossibleX(x, y, number) && isPossibleY(x,y, number)
            && isPossibleBlock(x, y, number)){       
        return number;
    }
    }
return -1;
}

And the error looks like :

Exception in thread "main" java.lang.StackOverflowError
at java.util.ArrayList.remove(ArrayList.java:474)
at qrczak.sudoku.SudokuBoard.getNextPossibleNumber(SudokuBoard.java:76)
at qrczak.sudoku.FillSudokuSolver.fill(FillSudokuSolver.java:39)
at qrczak.sudoku.FillSudokuSolver.solve(FillSudokuSolver.java:62)
at qrczak.sudoku.FillSudokuSolver.fill(FillSudokuSolver.java:45)
at qrczak.sudoku.FillSudokuSolver.solve(FillSudokuSolver.java:62)
at qrczak.sudoku.FillSudokuSolver.fill(FillSudokuSolver.java:45)
at qrczak.sudoku.FillSudokuSolver.solve(FillSudokuSolver.java:62)

I think that mistkake occure somewhere in that part of the code, but to be honest I was looking for it for 2 days and i can not find it. Almost the same code but without interfaces works fine !

share|improve this question
4  
Have a good look at the stack trace, and notice a pattern of alternating lines. Now find those lines in your code, and voilà :) –  fvu yesterday
    
Use a debugger and figure out why it keeps bouncing between fill() and solve() –  Danilo Tommasina yesterday
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.