Results 1 to 7 of 7
  1. #1
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Problem with Recursion method with stackOverFlow error

    Hey guys, sorry if the title is a bit weird.... didn't have enough space.

    So here's the thing, I have a code that has to create a checkboard type of array ( 2d) and used a recursive function to add every number at every "case". However, it has to evaluate the number to his left or right... Therefore if the case to the left ( so the row under the current one) is greater than the one to the right, than it will add it to its previous case value ( I.E I'm on case with a 4 and there is a chance to go on 1 or 10 --> I will have 14 afterwards).Sorry my explanation is kinda bad...

    Anyway, I created my function and when I run it I get a "Exception in thread "main" java.lang.StackOverflowError
    at assignment3.CheckBoard.checkerRecursion(CheckBoard .java:55)"

    So... I know that this usually happens when there is too much calls on the recursive method and it takes all the stack memory of the java virtual machine ( or so this is what I found with google's help!)

    and now... here's my code.:


    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class CheckBoard {
    	public static void main(String[] args){
    		Scanner kb = new Scanner(System.in);
    		System.out.println("Please input the column at which you'd like to start the program:");
    		int startingColumn = kb.nextInt();
    		System.out.println("The system will now initialize an array of 9x9 size, and will start at row 0");
    		int passedArray[][] = new int[9][9];
    		Random rand = new Random(); 
    		for (int i=0; i < passedArray.length; i++){
    			for(int g=0; g<passedArray[i].length; g++)
    			{
    				passedArray[i][g] = rand.nextInt((11) -0)+0;
    				System.out.print(passedArray[i][g] + "       ");
    			}
    		System.out.println("");
    		}
    		int startingRow = 0;
    		int sum = passedArray[0][startingColumn];
    		int totalSum = checkerRecursion(passedArray,startingRow,startingColumn, sum);
    		System.out.println(totalSum);
    		
    	}
    	public static int checkerRecursion(int checkBoardArray[][], int startingRow, int startingColumn, int sum){
    		/**
    		 * if statements will execute only at the last row of the array
    		 */
    		if(checkBoardArray.length == startingRow + 1){
    			return sum;
    		}
    		/**
    		 * else if statement checks whether or not we're at the last row
    		 */
    		else if(startingRow  < checkBoardArray.length - 1 ){
    			/**
    			 * this if statement will only evaluate if the case on the column to the left of the current one, and on the next row
    			 * is greater  than the opposite one. Also, it will only occur if there is a column to the left and not a -1
    			 */
    			if (checkBoardArray[startingRow+1][startingColumn-1] > checkBoardArray[startingRow+1][startingColumn+1] && checkBoardArray[startingRow].length- 1 > 0){ 
    				sum += checkBoardArray[startingRow+1][startingColumn-1];
    				startingRow++;
    				startingColumn--;
    			}
    			/**
    			 * this if statement will only evaluate if the case on the column to the right of the current one, and on the next row
    			 * is greater  than the opposite one. Also, it will only occur if there is a column to the right and not an array out of bound.
    			 */
    			else if(checkBoardArray[startingRow+1][startingColumn-1] < checkBoardArray[startingRow+1][startingColumn+1] && startingColumn + 1 == checkBoardArray[startingRow].length  )
    			{
    				sum += checkBoardArray[startingRow+1][startingColumn+1];
    				startingRow++;
    				startingColumn ++;
    			}
    			else if (checkBoardArray[startingRow+1][startingColumn-1] == checkBoardArray[startingRow+1][startingColumn+1] && checkBoardArray[startingRow].length- 1 > 0){
    				sum += checkBoardArray[startingRow+1][startingColumn-1];
    				startingRow++;
    				startingColumn--;
    			}
    			else if(checkBoardArray[startingRow+1][startingColumn-1] == checkBoardArray[startingRow+1][startingColumn+1] && startingColumn + 1 == checkBoardArray[startingRow].length){
    				sum += checkBoardArray[startingRow+1][startingColumn+1];
    				startingRow++;
    				startingColumn ++;
    			}
    			checkerRecursion(checkBoardArray, startingRow,startingColumn,sum);
    			
    		}
    		return sum;
    	}
    }

    thanks for the help!
    Last edited by DarrylBurke; 08-02-2015 at 09:58 AM. Reason: Would like to edit my title. Forgot to add exception to it :/

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Problem with Recursion method with stackOverFlow error

    I've edited your post to change block comments /* ... */ to doc comments /** ... */ as the forum software has a known bug with formatting block comments.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Problem with Recursion method with stackOverFlow error

    How are you trying to debug the code?
    It might be faster debugging to make a smaller 2D array, say 3x3 vs 9x9
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Re: Problem with Recursion method with stackOverFlow error

    I run it with a 3x3 and it gave me an array out of bound error. BUT, when fixed, again, it gives me stackOverFlow. I don't know how to fix that. Does it just mean my recursion method doesnt work

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Problem with Recursion method with stackOverFlow error

    Quote Originally Posted by Manddd View Post
    Does it just mean my recursion method doesnt work
    Well, yes. Clearly you are not properly returning from the method so the call stack reaches its limit and results in the thrown exception. I recommend you put in some print statements as some debugging aids. Make them class variables so they don't get reset each time you enter the method. You might also want to catch the exception to reduce the clutter (but get the line number of the thrown exception to further facilitate debugging).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Re: Problem with Recursion method with stackOverFlow error

    Never mind, fixed it! I was wondering, when I give java an instruction, such as:
    Java Code:
     (startingColumn -1 >=0  &&  startingColumn  + 1 < checkBoardArray[startingRow].length
     && checkBoardArray[startingRow+1][startingColumn-1] >
     checkBoardArray[startingRow+1][startingColumn+1])
    Java Code:
    ( checkBoardArray[startingRow+1][startingColumn-1] > checkBoardArray[startingRow+1][startingColumn+1] 
    
    && startingColumn -1 >=0)
    1st, in the seccond piece of code, does java analyze the left hand side first? If so, doesn't switching around ( so the startingColumn - 1 >= 0 ) would fix the issue of java telling me " There's an array out of bound, so we can't compare". But if the left hand side is already false, no need to analyze the following, no? Anyway , the only to fix my error was to create a specific case for every different possibilities of the checker.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Problem with Recursion method with stackOverFlow error

    What you are describing is the short circuit version of the && operator. If the first part is false, no sense in evaluating the second part. If you use &, both parts will be evaluated regardless of the result of the left side. But I would avoid things like this:

    Java Code:
    if (a > 0 && b++ < 10) {
      // do something
    }
    Because it can introduce bugs that are hard to find.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. Java Recursion method
    By Risvosk in forum New To Java
    Replies: 3
    Last Post: 12-16-2011, 05:30 PM
  3. Replies: 3
    Last Post: 04-12-2011, 06:44 AM
  4. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM
  5. My first recursion method: the number e
    By Lil_Aziz1 in forum Reviews / Advertising
    Replies: 1
    Last Post: 01-23-2010, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •