please help
-
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)
-
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)
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
-
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
-
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)
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.
-
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.
-
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???
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 wheni
is equal to 3,i+1
will evaluate to 4 and still be a valid index. -
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)
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