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

1. ## A little help

So I have an infinite loop on my hands, and I was wondering if anyone can help me out. My purpose is to make an iterative cube root function using newtons method. Any help would be greatly appreciated.

I set a = 1.0 just to test. I need to go through numbers 1 -50 but thats really irrelavant.

```    public static void cRoot ()
{
///newtons method for compution.
boolean closeEnough = false;
double X, XX;
double a = 1.0;
/// set the cubeRoot for 0 and 1.
if (a == 0.0)
X = 0.0;
if (a == 1.0)
X = 1.0;

X = a/2.0;  // first guess.
while( closeEnough == false )
{
// Next guess
XX = (1/3.0 *( 2.0 * Math.pow(X, 3) + a) / (Math.pow(X, 2)));
// Is it close enough?
if ( (XX - X)/X < 0.00001)
// New guestimate.
X = XX;
}
/// Output of the cuberoot.
System.out.print(X);
}

}```

2. ## Re: A little help

According to your code, you have a while loop that is essentially
```while(true){
//
}```
because you never change the closeEnough variable. Possible fix:
```if ( (XX - X)/X < 0.00001)
{
break;
}```
use a break statement.

3. ## Re: A little help

Thank you seems to have opened some doors. Thanks.