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 7 of 7

Thread: ArrayOutofBoundary in Simple While Loop

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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.");
     }
    }
    Last edited by helloworld922; May 28th, 2010 at 10:00 PM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default 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:
    for( i = 1; i < limit; i++ )
    wich I think may be a problem. Try
    for( i = 1; i <= limit; i++ )

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,002
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: ArrayOutofBoundary in Simple While Loop

    while( i < limit )
    {
    i++;
    What happens here if i = limit-1

  5. The Following User Says Thank You to Norm For This Useful Post:

    JavaPF (June 18th, 2010)

  6. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayOutofBoundary in Simple While Loop

    Quote Originally Posted by Norm View Post
    What happens here if i = limit-1
    Norm hit the nail on the head. You increment i before you actually use i, so
    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?

  7. The Following User Says Thank You to copeg For This Useful Post:

    JavaPF (June 18th, 2010)

  8. #6
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  9. #7
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: ArrayOutofBoundary in Simple While Loop

    hello, delete the i++ in the while loop and use while( i++ < limit ) instead.

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  3. not so simple, simple swing question box
    By wolfgar in forum AWT / Java Swing
    Replies: 2
    Last Post: November 20th, 2009, 02:47 AM
  4. boolean value in a simple loop (do-while)
    By chronoz13 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 18th, 2009, 12:05 AM
  5. [SOLVED] Looping of particular instruction ith times
    By lotus in forum Loops & Control Statements
    Replies: 2
    Last Post: July 12th, 2009, 11:47 PM