Hi, I am a beginner so bear with me. I have an assignment in which I have to create an array with numbers and then have a loop with condition to check in which order the user has inputed the numbers. Asc=1, desc=-1, other=0. This loop isn't working, i know it's wrong but can't figure out what to do, I have experienced with lots of options still nothing works for me.

import java.util.Scanner;
public class Arrange {
	public static void main (String[] args){
		Scanner input= new Scanner(System.in);
		double numbers;
		double [] arr=new double[5];
		for (int i=0; i<5; i++){
			System.out.println("Enter your numbers ");
			numbers= input.nextDouble();
			arr[i]=numbers;
		
		}
			for(int i=0; i<4; i++){
				for (int j=1; j<5; j++){
				
			if (arr[i]<arr[j]){
				System.out.println ("1");
				
			}else if (arr[i]>arr[j]){
				System.out.println ("-1");
				
			}else{
				System.out.println("0");
				
			}}
		}
	}
}

Thanks!

Recommended Answers

All 8 Replies

Are you getting any errors or is the program not running as you expect it to ?

I am not getting any errors, it runs smoothly but the problem is that it repeats the (1,-1 or 0) as many times as the loop itself and it seems to compare each couple of numbers alone and not the whole list at once.

but the problem is that it repeats the (1,-1 or 0) as many times as the loop itself

It is because that you have included the print statement in the for loop and in each iteration it would print so. That is your logic.

HOwever if you need to see the ordering of the entered numbers you should make use of a temporary variable and handl this and the printing should be done after exiting the loop.

It is because that you have included the print statement in the for loop and in each iteration it would print so. That is your logic.

HOwever if you need to see the ordering of the entered numbers you should make use of a temporary variable and handl this and the printing should be done after exiting the loop.

Indeed, that was also the problem but I tried to fix it by using a variable value to assign the (1,-1,0) to and have an outside println to help reduce the number of times the results is printed out and it keeps returning an error that suggests: the value it's trying to print hasn't been initialized. Which is strange because I already declared it.

May I see your modified code please?

May I see your modified code please?

Yes, sure:

import java.util.Scanner;
public class Arrange {
	public static void main (String[] args){
		Scanner input= new Scanner(System.in);
		double numbers;
		double [] arr=new double[5];
		for (int i=0; i<5; i++){
			System.out.println("Enter your numbers ");
			numbers= input.nextDouble();
			arr[i]=numbers;
		
		}
		int s=0;
			for(int i=0; i<arr.length-1; i++){
				for (int j=1;j<arr.length-1; j++){
				
			if (arr[i]<arr[j]){
				s=1;
				break;
			}else if (arr[i]>arr[j]){
				s= -1;
				break;
			}else{
				s= 0;
				
			}}
		} System.out.println("The order was:"+ s);
	}
}

The problem with this is that it takes s=0 for granted and never check what came out of the loop.

OK, you want to check whether the whole array value is in ascending, descending, or otherwise? How do you consider if values entered are equal? The below algorithm should work with any consecutive equal values.

A simple solution is to have 2 variables to count, let say asc and desc. Initial both to 0. Iterate through the array from i=0; i<arr.length-1; i++ (notice that it is i<arr.length-1). If arr<=arr[i+1], then increase asc by 1. If arr>=arr[i+1] (notice that it is not else if), then increase desc by 1. After the loop is done, check the value of asc and desc. If asc value is equal to the length of arr minus 1, it is ascending. If desc value is equal to the length of arr minus 1 (notice that it is not else if again), it is descending. If neither, it is other.

PS: This algorithm does NOT work with an array size of 1.

commented: Very helpful! Thank you +1

OK, you want to check whether the whole array value is in ascending, descending, or otherwise? How do you consider if values entered are equal? The below algorithm should work with any consecutive equal values.

A simple solution is to have 2 variables to count, let say asc and desc. Initial both to 0. Iterate through the array from i=0; i<arr.length-1; i++ (notice that it is i<arr.length-1). If arr<=arr[i+1], then increase asc by 1. If arr>=arr[i+1] (notice that it is not else if), then increase desc by 1. After the loop is done, check the value of asc and desc. If asc value is equal to the length of arr minus 1, it is ascending. If desc value is equal to the length of arr minus 1 (notice that it is not else if again), it is descending. If neither, it is other.

PS: This algorithm does NOT work with an array size of 1.

I am so happy, it is working perfectly, might be a bit long but at least now I have a better insight. Thank you very very much, you saved my life. :D
Here's the final code I have, will alter it to make it more efficient though:

import java.util.Scanner;
public class Arrange {
	public static void main (String[] args){
		Scanner input= new Scanner(System.in);
		double numbers;
		double [] arr=new double[5];
		for (int i=0; i<5; i++){
			System.out.println("Enter your numbers ");
			numbers= input.nextDouble();
			arr[i]=numbers;}
		
		int asc=0;
		int desc=0;
		
		for(int i=0; i<arr.length-1; i++){
			if (arr[i]<=arr[i+1]){
				asc+=1;
			}
			if (arr[i]>=arr[i+1]){
				desc+=1;
			}
		}
		if (asc==arr.length-1){
			System.out.println("order is ascending 1");
			
		}if (desc==arr.length-1){
			System.out.println("order is descending -1");
			
		}if((asc!=arr.length-1)&&(desc!=arr.length-1)){
			System.out.println("0");
		}
		}
	}

Thank you! Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.