Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. please help

please help

Scheduled Pinned Locked Moved Java
helpjavaquestion
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OmarSH
    wrote on last edited by
    #1

    some one please tell me what is the problem here is the code

            public class sortm {
    public static void main(String args\[\])
    {
    	
    	int a\[\]={5,3,7,2,8};
    	int b\[\]={1,9,4,8,10};
    	int i=1;
    	int j=1;
    	int t=0;
    	int s=0;
    	for( i=1;i<=5;i++)
    	{
    		if(a\[i\]>a\[i+1\])
    			t=a\[i\];
    		a\[i\]=a\[i+1\];
    		a\[i+1\]=t;
    		
    	}
    	for( i=1;i<=5;i++){
    	System.out.println(a\[i\]);
    	}
    	
    	for(j=0;j<=5;j++)
    	{
    		if(b\[j\]>b\[j+1\])
    			s=b\[j\];
    		b\[j\]=b\[j+1\];
    		b\[j+1\]=s;
    	}
    	for(j=0;j<=5;j++)
    	{
    		System.out.println(b\[j\]);
    	}
    }
    

    }

    and this what running say : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at sortm.main(sortm.java:14)

    V J P 3 Replies Last reply
    0
    • O OmarSH

      some one please tell me what is the problem here is the code

              public class sortm {
      public static void main(String args\[\])
      {
      	
      	int a\[\]={5,3,7,2,8};
      	int b\[\]={1,9,4,8,10};
      	int i=1;
      	int j=1;
      	int t=0;
      	int s=0;
      	for( i=1;i<=5;i++)
      	{
      		if(a\[i\]>a\[i+1\])
      			t=a\[i\];
      		a\[i\]=a\[i+1\];
      		a\[i+1\]=t;
      		
      	}
      	for( i=1;i<=5;i++){
      	System.out.println(a\[i\]);
      	}
      	
      	for(j=0;j<=5;j++)
      	{
      		if(b\[j\]>b\[j+1\])
      			s=b\[j\];
      		b\[j\]=b\[j+1\];
      		b\[j+1\]=s;
      	}
      	for(j=0;j<=5;j++)
      	{
      		System.out.println(b\[j\]);
      	}
      }
      

      }

      and this what running say : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at sortm.main(sortm.java:14)

      V Offline
      V Offline
      venky0028
      wrote on last edited by
      #2

      in the above code you stored array with five elements so yor index range from 0 to 4 you mention i=1 and maxmium is 5 so for iterations i become 5 at that time it will give arrayoutbound exception you can just change for( i=1;i<5;i++) { if(a[i]>a[i+1]) t=a[i]; a[i]=a[i+1]; a[i+1]=t; } it will execute

      O 1 Reply Last reply
      0
      • V venky0028

        in the above code you stored array with five elements so yor index range from 0 to 4 you mention i=1 and maxmium is 5 so for iterations i become 5 at that time it will give arrayoutbound exception you can just change for( i=1;i<5;i++) { if(a[i]>a[i+1]) t=a[i]; a[i]=a[i+1]; a[i+1]=t; } it will execute

        O Offline
        O Offline
        OmarSH
        wrote on last edited by
        #3

        sorry but you solution didnt work, there is problem in in this statement a[i]>a[i+1] ,when i be 5 ,it is in a[5]>a[6], so compiler search for a[6] and dont found,so here gave me the problem ,and by the way what is a[i]>a[i+1] i didnt get it thank you

        1 Reply Last reply
        0
        • O OmarSH

          some one please tell me what is the problem here is the code

                  public class sortm {
          public static void main(String args\[\])
          {
          	
          	int a\[\]={5,3,7,2,8};
          	int b\[\]={1,9,4,8,10};
          	int i=1;
          	int j=1;
          	int t=0;
          	int s=0;
          	for( i=1;i<=5;i++)
          	{
          		if(a\[i\]>a\[i+1\])
          			t=a\[i\];
          		a\[i\]=a\[i+1\];
          		a\[i+1\]=t;
          		
          	}
          	for( i=1;i<=5;i++){
          	System.out.println(a\[i\]);
          	}
          	
          	for(j=0;j<=5;j++)
          	{
          		if(b\[j\]>b\[j+1\])
          			s=b\[j\];
          		b\[j\]=b\[j+1\];
          		b\[j+1\]=s;
          	}
          	for(j=0;j<=5;j++)
          	{
          		System.out.println(b\[j\]);
          	}
          }
          

          }

          and this what running say : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at sortm.main(sortm.java:14)

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          OmarSH wrote:

          some one please tell me what is the problem

          Your arrays have indexes from 0 to 4. So 0, 1, 2, 3, 4 are the ONLY valid values. Print out 'i' and print out 'i+1' BEFORE you use it as an array index. If either of those are greater than 4 then it is a bug. Print out 'j' and print out 'j+1' BEFORE you use it as an array index. If either of those are greater than 4 then it is a bug.

          O 1 Reply Last reply
          0
          • J jschell

            OmarSH wrote:

            some one please tell me what is the problem

            Your arrays have indexes from 0 to 4. So 0, 1, 2, 3, 4 are the ONLY valid values. Print out 'i' and print out 'i+1' BEFORE you use it as an array index. If either of those are greater than 4 then it is a bug. Print out 'j' and print out 'j+1' BEFORE you use it as an array index. If either of those are greater than 4 then it is a bug.

            O Offline
            O Offline
            OmarSH
            wrote on last edited by
            #5

            iknow that but i want to sort array as the code show but compiler when a[i] reach the final one a[4] it compare it with a[5] which it is not exist , i want to it stop compare when it reach the final index is there any code that could help me???

            L 1 Reply Last reply
            0
            • O OmarSH

              iknow that but i want to sort array as the code show but compiler when a[i] reach the final one a[4] it compare it with a[5] which it is not exist , i want to it stop compare when it reach the final index is there any code that could help me???

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              OmarSH wrote:

              i want to it stop compare when it reach the final index

              Then check the value of the index before the compare. Or change your for statement so it counts from 0 to 3, then when i is equal to 3, i+1 will evaluate to 4 and still be a valid index.

              1 Reply Last reply
              0
              • O OmarSH

                some one please tell me what is the problem here is the code

                        public class sortm {
                public static void main(String args\[\])
                {
                	
                	int a\[\]={5,3,7,2,8};
                	int b\[\]={1,9,4,8,10};
                	int i=1;
                	int j=1;
                	int t=0;
                	int s=0;
                	for( i=1;i<=5;i++)
                	{
                		if(a\[i\]>a\[i+1\])
                			t=a\[i\];
                		a\[i\]=a\[i+1\];
                		a\[i+1\]=t;
                		
                	}
                	for( i=1;i<=5;i++){
                	System.out.println(a\[i\]);
                	}
                	
                	for(j=0;j<=5;j++)
                	{
                		if(b\[j\]>b\[j+1\])
                			s=b\[j\];
                		b\[j\]=b\[j+1\];
                		b\[j+1\]=s;
                	}
                	for(j=0;j<=5;j++)
                	{
                		System.out.println(b\[j\]);
                	}
                }
                

                }

                and this what running say : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at sortm.main(sortm.java:14)

                P Offline
                P Offline
                pranay1988
                wrote on last edited by
                #7

                the arrays index no start with zero, and you enter here 5 element in array, so the last elent index no is 4 and you take loop upto 5 index no hense it raised error array index out of bound int a[]={1,2,3,4,5]; for(inti=0;i<5;i++) System.out.println(a[i]); or for(int i=0;i<=4;i++) or for(int i=0;i

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups