Results 1 to 4 of 4
  1. #1
    simar is offline Member
    Join Date
    Aug 2014
    Posts
    24
    Rep Power
    0

    Default error in ascending order prog of java

    tfrreee
    i want to display given series in ascending order in java prog can anybody help me out wil b thnkful here is an error : E:\Java\jdk\bin>java ascend 505671Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at ascend.main(ascend.java:9)
    Java Code:
     class ascend 
    { 
    public static void main (String args[])
     {
     int s[]={ 50,71,81,21,7};
     int i;
     for(i=0;i<=s.length;i++)
     { 
    if(s[i]<s[i 1])
     System.out.print(s[i]);
     }
     } 
    }
    here below is the corrected code:
    Java Code:
    class ascend
    {
    public static void main (String args[])
    {
    int s[]={ 50,71,81,21,7};
    int i;
    for(i=0;i<=s.length;i++)
    {
    int temp;
    for(int j=i+1;j<s.length;j++)
    {
    
    if(s[i]>s[j])
    {
    temp=s[i];
    s[i]=s[j];
    s[j]=temp;
    }
    }
    }
    System.out.println();
    System.out.print("After Sorting in  ascending order: ");
    for(i=0;i<s.length;i++)
    {
    System.out.print(" "+s[i]);
    }
    }
    }
    Last edited by simar; 08-10-2014 at 07:02 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: error in ascending order prog of java

    The array only has 5 elements. Obviously 7 is not a valid index.

  3. #3
    simar is offline Member
    Join Date
    Aug 2014
    Posts
    24
    Rep Power
    0

    Default Re: error in ascending order prog of java

    its solved out my logic was wrong ,,,,,i want to delete this thread but dont know how,,,,,anyone knows.......

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: error in ascending order prog of java

    You can't delete the thread. I suggest you reformat your code and then explain how you solved the problem so others
    might benefit.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. ascending order or not in ascending order
    By hawkesey in forum New To Java
    Replies: 7
    Last Post: 01-16-2013, 12:56 PM
  2. Replies: 8
    Last Post: 11-23-2012, 04:39 PM
  3. search array - ascending order
    By wiola111 in forum New To Java
    Replies: 5
    Last Post: 08-28-2012, 03:09 PM
  4. Sorting in ascending and descending order
    By flpanthers1 in forum New To Java
    Replies: 10
    Last Post: 06-27-2011, 03:48 PM
  5. ascending order using array rush
    By jca in forum New To Java
    Replies: 2
    Last Post: 01-03-2011, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •