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

# Thread: Recursion Stackoverflow Error help!

1. ## 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. ## 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)