Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Recursion Stackoverflow Error help!

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursion Stackoverflow Error help!

    public class Project02
    {
    	private double areaTotal;
    	public Project02(double Total)
    	{
    		areaTotal = Total;
    	}
    		public static double TotalArea(double areaTotal)
    		{		
    			double []x = {7.5774, 6.4226, 4.3453, 3.4226, 4.5724, 6.6547};
    			double []y = {4.6340, 6.3660, 6.2321, 4.3660, 2.6340, 2.7679};
     
    			double a1 = (x[0] * y[1] + x[1] * y[2] + x[2] * y[0] - y[0] * x[1] - y[1] * x[2] - y[2] * x[0])/2;
    			double a2 = (x[0] * y[2] + x[2] * y[3] + x[3] * y[0] - y[0] * x[2] - y[2] * x[3] - y[3] * x[0])/2;
    			double a3 = (x[0] * y[3] + x[3] * y[4] + x[4] * y[0] - y[0] * x[3] - y[3] * x[4] - y[4] * x[0])/2;
    			double a4 = (x[0] * y[4] + x[4] * y[5] + x[5] * y[0] - y[0] * x[4] - y[4] * x[5] - y[5] * x[0])/2;
     
    			double []a = {a1, a2, a3, a4};
     
    			double result;
    			if(areaTotal == 0 && areaTotal > 3)
    				{
    				result = 0;
    				System.out.println("Done");
    				}
    			else
    				{
    				result = a[(int)areaTotal] + TotalArea(a[(int)areaTotal - 1]);
    				System.out.println(result);
    				}
    				return result;
    	}
    }

    DRIVER
    import java.util.Scanner;
     
    public class Project02Driver
    {
    	public static void main(String []args)
    	{
    		int areaTotal;
    		Scanner scan = new Scanner(System.in);
    		areaTotal = scan.nextInt();
    		System.out.println(Project02.TotalArea(areaTotal));
    	}
    }

    I run this program using the driver class. However, every time I input a number for areaTotal I get a stackoverflow error. My end goal in this program is to be able to use recursion to successfully print the total in array a using my driver program.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: Recursion Stackoverflow Error help!

    Not sure if it's the problem, but i found something suspicious. Look at the following statement. What are you trying to achieve here?
    		if(areaTotal == 0 && areaTotal > 3)
    When would this return true and when would this return false?

  3. The Following User Says Thank You to elamre For This Useful Post:

    awinston (July 28th, 2012)

Similar Threads

  1. StackOverFlow Error
    By ankiit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 13th, 2012, 08:49 AM
  2. BlinkingLabel-StackOverflow Error
    By MichaelWU in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 15th, 2011, 08:25 AM
  3. Why am I getting StackOverflow errors?
    By techcom0 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 12th, 2011, 09:18 AM
  4. Recursion
    By javapenguin in forum Algorithms & Recursion
    Replies: 12
    Last Post: October 18th, 2010, 03:42 PM
  5. Recursion help
    By rhoruns in forum Algorithms & Recursion
    Replies: 4
    Last Post: January 8th, 2010, 10:50 PM