Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My output is as follows :
5
1 3 5 7 9
1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at myproject/myproject.MainExam3.main(MainExam3.java:35)
3
5
7
9

Expected:
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 30-Sep-20 9:16am
v3
Comments
[no name] 4-Sep-20 14:41pm    
Add a 7 and change the message.
ZurdoDev 30-Sep-20 17:19pm    
What is your question?

1 solution

Java
for(int i=0; i<a.length-1; i++)
{
    System.out.println(a[i]);
    if(a[i]>a[i+1])
    {
            flag=1;
            break;
    }
}

This will never print the last number because of a.length-1, but you need it to compare elements.
Java
if(flag==1)
{
    System.out.println("asc order");
}
else if(flag==0)
{
    System.out.println("not asc order");
}

May be the test is in wrong order.
Quote:
Whats was wrong in my code

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.

[Update]
This loop
Java
for(int i=0; i<a.length; i++)
{
    System.out.println(a[i]);
    if(a[i]>a[i+1])
    {
        flag=1;
        break;
    }
}

does 2 different things, 1 need to loop 5 times, the other needs to loop only 4 time.
 
Share this answer
 
v2
Comments
CPallini 1-Oct-20 2:05am    
5.
Patrice T 1-Oct-20 3:46am    
Thank you

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