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: Unending Loop Issue? Can't find it.

1. ## Unending Loop Issue? Can't find it.

Although I think this is an issue with my loops structure/conditions this could also be an issue with how Java does arithmetic. I'm sure this is probably simple but I can't find it right now.
The polynomial object is composed of an array of size 100 which stores the coefficients of a polynomial in index locations corresponding to the power of each respective term. The purpose of this code is to create a new polynomial object which is the result of multiplying two polynomials.
I'm pretty sure that the loop is not ending but I'm not sure why. The initial S.o.ps of i+j values are what I expect but then they go into enormous negative numbers and I have no idea why.
```  public Polynomial mult(Polynomial p)
throws ExponentOutOfRangeException
{
Polynomial multiPolynomial = new Polynomial();

//Start testcode
System.out.print("this polynomial ");
this.displayPolynomial();
System.out.print("p polynomial ");
p.displayPolynomial();
//End testcode

for(int i = 0; i < 99; i++)
{
for(int j = 0; j < 99; i++)
{
if((i+j) < MAX)
{
multiPolynomial.polyArr[i + j] = multiPolynomial.polyArr[i + j] + (this.polyArr[i] * p.polyArr[j]);
}
}
}
// Multiplies polynomial p to this polynomial without modifying this
// polynomial and returns the result.
// Precondition: None.
// Postcondition: The returned polynomial is the product of this
// and p.  Both this and p are unchanged.
// Throws: ExponentOutOfRangeException if exponent is out of range.
return multiPolynomial;
}```
By the way,

2. ## Re: Unending Loop Issue? Can't find it.

Thanks to whoever fixed the way the code displays here. I was looking up the necessary tags just now. Anyone see the problem? Maybe I should include more code...

3. ## Re: Unending Loop Issue? Can't find it.

`for(int j = 0; j < 99; i++)`
You're incrementing i in the j loop, not j

4. ## The Following User Says Thank You to helloworld922 For This Useful Post:

javapenguin (March 4th, 2011)

5. ## Re: Unending Loop Issue? Can't find it.

You seem to be incrementing i twice.

Perhaps you should change it to j++;

As it is, i is being incremented twice and j is stuck at 0.

Musta posted at same time as helloworld922.

Oh well.

6. ## Re: Unending Loop Issue? Can't find it.

Omigosh, how embarrassing! I looked at the problem is sooo many ways and I missed that simple item. I guess that's why you ask other people to look at code. Thanks guys!