When I run my code I get java.lang.StackOverflowError

I would be more than appreciative if someone can tell me what's wrong.

This is my code:

My assignment is to make a Sudoku. The strange thing is when I debug it everything is going well but I cant debug it forever..

Need help u all :(

public static void main(String[] args) {
    System.out.println("Welcome to java Sudoku to start the game press enter");
    Scanner scanner = new Scanner(System.in);
    scanner.nextLine();
    int AllNum=1000;
    TestRow=0;
    TestColumn=0;
    TheNumUPut=0;
    finish=false;

            while(AllNum>99){
                    AllNum =GivenANum();
                    if(AllNum>99)NumInPlace(AllNum);}//end while


            SolveSudoku();  
}// end main


public static void SolveSudoku() {

            if (TestRow==9&&TestColumn==0)PrintSudoku();

            if(finish==false){
                if (SolveOneCell()==true){
                    TestColumn++;
                    if(TestColumn==9) {TestColumn=0; TestRow++;}}
                else
                    ReturnToLast();

                    SolveSudoku();
            }

}// end Solve Sudoku


public static boolean SolveOneCell( ){// הפונקציה פותרת את הסודוקו לאחר שהמשתמש הכניס את המספרים שלו
                if(IsGiven()==true) return true;

                IncreaseNum();

                while (TheNumUPut<11){
                if(TheNumUPut>9){PutZero(); return false;}

                if(UCanPut()==true){Put();return true;}
                else
                    TheNumUPut++;}

    return true;
    }// end solve

public static boolean ReturnToLast() {
    while(1>0){
            if(TestColumn==0)
                {
                    TestColumn=8;
                    TestRow=TestRow-1;
                }
            else TestColumn=TestColumn-1;

            if(IsGiven()==false) break;
    }

    return true;
}// end return to NON last given



public static int GivenANum(){// הפונקציה קולטת מספר מהמשתמש
        int PutANum;
        System.out.println("Please enter a given number and its location ?");
        PutANum = reader.nextInt();
        return PutANum;
}// end givens

public static void NumInPlace(int TheNum){// הפונקציה שמה את המספר שנקלט במקום הנכון על הלוח
    Row = (TheNum/10)%10;
    Column= TheNum%10;
    TheGivenNumber= (TheNum/100)%10;
    TheBoard [Row][Column]=TheGivenNumber;  
    TheGivenBoard[Row][Column]=TheGivenNumber;
}// end NumInPlace



public static void Put(){// שם בתא את המספר
    if(TestRow>(-1)&&TestColumn>(-1)){
        TheBoard[TestRow][TestColumn]=TheNumUPut;   }

}// end Put



public static void PutZero(){// שם 0 בתא
    if(TestRow<9&&TestColumn<9)  TheBoard[TestRow][TestColumn]=0;   
}// end PutZero



public static void IncreaseNum(){// מגדילה את המספר ב1
    if(TestRow<9&&TestColumn<9)  TheNumUPut=TheBoard[TestRow][TestColumn]+1;    
}// end increase



public static boolean IsGiven(){// הפונקציה בודקת אם במקום יש מספר שהמשתמש הכניס
    if(TestRow>8 || TestColumn>8) return false;
    if (TheGivenBoard[TestRow][TestColumn]!=0) return true;
    return false;
}// end Not given


public static boolean UCanPut(){// הפונקציה בודקת אם ניתן להכניס את המספר
    if (NumInRow()==true) return false;
    if (NumInColumn()==true) return false;
    if(InSameSquer(WhatSquare())==true) return false;
    return true;
}// end U can put


public static boolean InSameSquer(int thesquare){// בודקת איזה ריבוע נמצא התא
    int i,j;

    if(thesquare==1){
            for (i=0;i<3;i++){
                for (j=0;j<3;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 1
    if(thesquare==2){
        for (i=0;i<3;i++){
            for (j=3;j<6;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 2
    if(thesquare==3){
        for (i=0;i<3;i++){
            for (j=6;j<9;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 3
    if(thesquare==4){
        for ( i=3;i<6;i++){
            for ( j=0;j<3;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 4
    if(thesquare==5){
        for ( i=3;i<6;i++){
            for (j=3;j<6;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 5
    if(thesquare==6){
        for (i=3;i<6;i++){
            for (j=6;j<9;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 6
    if(thesquare==7){
        for (i=6;i<9;i++){
            for (j=0;j<3;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 7
    if(thesquare==8){
        for ( i=6;i<9;i++){
            for ( j=3;j<6;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 8
    if(thesquare==9){
        for ( i=6;i<9;i++){
            for ( j=6;j<9;j++) if(TheBoard[i][j]==TheNumUPut) return true;}}//סוף בדיקה 9
return false;
}



public static int WhatSquare(){// בודקת איזה ריבוע נמצא התא
    int Square=0;
    if(TestRow<3&&TestColumn<3) Square=1;
    if(TestRow<3&&TestColumn>2&&TestColumn<6) Square=2;
    if(TestRow<3&&TestColumn>5&&TestColumn<9) Square=3;
    if(TestRow>2&&TestRow<6&&TestColumn<3) Square=4;
    if(TestRow>2&&TestRow<6&&TestColumn>2&&TestColumn<6) Square=5;
    if(TestRow>2&&TestRow<6&&TestColumn>5&&TestColumn<9) Square=6;
    if(TestRow>5&&TestColumn<3) Square=7;
    if(TestRow>2&&TestColumn>2&&TestColumn<6) Square=8;
    if(TestRow>2&&TestColumn>5&&TestColumn<9) Square=9;

return Square;
}


public static boolean NumInRow(){
for (int i =0;i<9&&TestRow<9;i++){
    if(TheBoard[TestRow][i]==TheNumUPut)return true;}
return false;
}

public static boolean NumInColumn(){
for (int j =0;j<9&&TestColumn<9;j++){
    if(TheBoard[j][TestColumn]==TheNumUPut)return true;}
return false;
}

public static void PrintSudoku(){
    for (int i =0;i<9;i++){
        System.out.println();
            for (int j=0;j<9;j++){
                System.out.print(" "+ TheBoard[i][j]+" ");}
    }
    finish=true;
}

}//end all
有帮助吗?

解决方案

A stack overflow:

When a program attempts to use more space than is available on the call stack 
(that is, when it attempts to access memory beyond the call stack's bounds, 
which is essentially a buffer overflow), the stack is said to overflow, typically
 resulting in a program crash.

Your recursion is probably too deep, try replacing it with a while / for loop. :)

其他提示

Hard to tell but your example contains a recursive method and most StackOverflowErrors are due to badly handled recursion.

In this case, finish is (probably) alwaysfalse, so SolveSudoku(); throws StackOverFlowError once it's called itself enough times.

You're probably causing endless recursion.
Add a printout statement (System.out.println) at the start
of each method and see which one is called lots of lots of times.
This should pinpoint the exact problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top