14,667,169 members
Rate this:
See more:
My output is as follows :
5
1 2 5 6 8
1
2
5
6
8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at myproject/myproject.MainExam3.main(MainExam3.java:35)

Expected output :
5
1 3 5 7 9
1
3
5
7
9
ascending order

What I have tried:

```package myproject;
import java.util.*;

public class MainExam3 {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

int[] a = new int[n];
for(int i=0; i<n; i++)
{
a[i] = sc.nextInt();
}
int temp = 0;
for(int i=0; i<a.length; i++)
{
for(int j=i+1; j<a.length; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;

}
}
}

int flag = 0;
for(int i=0; i<a.length; i++)
{
System.out.println(a[i]);
if(a[i]>a[i+1])
{
flag=1;
break;
}
}
if(flag==1)
{
System.out.println("asc order");
}
else if(flag==0)
{
System.out.println("not asc order");
}
}

}```
Posted
Updated 5-Sep-20 4:56am

Rate this:

## Solution 1

Think about your loop end condition and what happens here
`if(a[i]>a[i+1])`

You have an array, of 5 elements (integers), when the loop has gone through i = 0,1,2,3,4 it tries to compare a[4] > a[5] and a[5] doesn't exist, bing bada boom !!!

From this, you should be able to fix the problem ..

btw, when you post a code dump like this and don't comment where the error is thrown from / which is line 35, you're not doing yourself any favours !!!
Yaswanth Kummar 5-Sep-20 9:50am

How to fix that error
Rate this:

## Solution 2

As Garth shared, it's difficult for anyone to help if you don't point the line you are getting error from.

Now, my suspects:
#1
```for(int i=0; i<a.length; i++)
{
for(int j=i+1; j<a.length; j++)
{
if(a[i]>a[j])```

Believe you are trying a selection sort here. Given the values, at the last value of i, j would go out of bound and can raise an error.
Example:
```a = "abcd"; // last index of a is a[3]
i=> 0,1,2,3
j=> 1,2,3,4
a[4] // when j=4 will bomb```

Fix:
```for(int i=0; i<a.length-1; i++)  //Notice the i value will go 1 less here
{
for(int j=i+1; j<a.length; j++)
{
if(a[i]>a[j])```

#2
```for(int i=0; i<a.length; i++)
{
System.out.println(a[i]);
if(a[i]>a[i+1])```

Here again, with the last value of i, i+1 will bomb.
Example:
```a = "abcd"; // last index of a is a[3]
i=> 0,1,2,3
i+1 => 1,2,3,4
a[4] // when i=3 will bomb```

Fix:
```for(int i=0; i<a.length-1; i++) //Notice the i value will go 1 less here
{
System.out.println(a[i]);
if(a[i]>a[i+1])```

Try out!
Rate this:

## Solution 3

As already stated in your other question: Whats was wrong in my code[^]
```int flag = 0;
for(int i=0; i<a.length; i++) // this loop runs 5 times
{
System.out.println(a[i]); // this needs to run 5 times
if(a[i]>a[i+1]) // and this needs to run only 4 times
{
flag=1;
break;
}
}
```

You can't have a loop with 2 parts needing to run a different number of times.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 165 Sandeep Mewara 70 KarstenK 65 PIEBALDconsult 55 Patrice T 52
 OriginalGriff 4,941 Richard MacCutchan 3,405 CPallini 2,082 Sandeep Mewara 1,525 Patrice T 1,256

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100