# ArrayOutofBoundary in Simple While Loop

• May 28th, 2010, 01:32 PM
Johnpower
ArrayOutofBoundary in Simple While Loop
Dear Friends:

The if statement in the while loop Line 50 is always wrong. I don't see anything wrong in it. If a pair of same numbers are put in the array, it runs well. If there is no such a pair the array is out of boundary. Could you see and fix the problem?

Thank you very much and the best regards!

John
Code :

```import java.io.*; import java.math.*; import cs1.Keyboard; public class Try{ public static void main(String[] args){   int i, limit = 11; BigInteger[]C = new BigInteger[limit]; C[1] = new BigInteger("1004"); C[2] = new BigInteger("1000"); C[3] = new BigInteger("1124"); C[4] = new BigInteger("1034"); C[5] = new BigInteger("1035"); C[6] = new BigInteger("1023"); C[7] = new BigInteger("1001"); C[8] = new BigInteger("1101"); C[9] = new BigInteger("1122"); C[10] = new BigInteger("1114");   System.out.println(" They are sorted as below:\n"); BigInteger min = new BigInteger("0"); BigInteger temp = new BigInteger("0"); int j,minindex; for( i = 1; i< limit; i++ ) { min = C[i]; minindex = i; for(j = i+1; j < limit; j++ ) if( C[j].compareTo(min) < 0 ) { min = C[j]; minindex = j; }   if( min.compareTo(C[i]) < 0 ) { temp = C[i]; C[i] = min; C[minindex] = temp; } } for( i = 1; i < limit; i++ ) System.out.println(" C["+i+"] = "+C[i]);   System.out.println(" Stop where same numberw occur:\n"); i = 0; while( i < limit ) { i++; if(C[i].equals(C[i-1])) { System.out.println(" C["+i+"] = C["+(i-1)+" = "+C[i]); break; } System.out.println(" C["+i+"] = "+C[i]); } System.out.println(" Done."); } }```
• May 28th, 2010, 02:09 PM
Bryan
Re: ArrayOutofBoundary in Simple While Loop
I didn't really give the code a good look but my first was at the array and it started with 1, normally a array starts at 0, and I saw the code:
Code :

`for( i = 1; i < limit; i++ )`
wich I think may be a problem. Try
Code :

`for( i = 1; i <= limit; i++ )`
• May 28th, 2010, 06:57 PM
Johnpower
Re: ArrayOutofBoundary in Simple While Loop
Thank you Bryan. I don't have problem with that line. If you copy and paste the whole code on Source Editor, the problem is on Line 50: if (C[ i ].equals( C[ i - 1 ] ). Nothing seems to be wrong here, but it is so after it is run.

Regards.

John
• May 28th, 2010, 08:10 PM
Norm
Re: ArrayOutofBoundary in Simple While Loop
Quote:

while( i < limit )
{
i++;
What happens here if i = limit-1
• May 28th, 2010, 08:21 PM
copeg
Re: ArrayOutofBoundary in Simple While Loop
Quote:

Originally Posted by Norm
What happens here if i = limit-1

Norm hit the nail on the head. You increment i before you actually use i, so
Code :

```while ( i < limit ){//when i = limit - 1 i++; //now i = limit. C[11] is out of bounds of the array```

Why not just use a for loop rather than a while loop?
• May 30th, 2010, 07:32 PM
Johnpower
Re: ArrayOutofBoundary in Simple While Loop
Dear Copeg, Norm, and Bryan:

I tried while( i < ( limit - 1 )) and it works! Thank you very much indeed for your clever suggestion! Have a nice day!

Regards.

John
• June 13th, 2010, 04:31 AM
j2me64
Re: ArrayOutofBoundary in Simple While Loop
hello, delete the i++ in the while loop and use while( i++ < limit ) instead.