Click here to Skip to main content
15,888,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

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 !!!
 
Share this answer
 
Comments
Yaswanth Kummar 5-Sep-20 9:50am    
How to fix that error
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
Java
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:
Java
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:
Java
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
Java
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:
Java
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:
Java
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!
 
Share this answer
 
As already stated in your other question: Whats was wrong in my code[^]
Java
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900