How can Sort 5 numbers in Java from high to low with if statements?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 5043 Views - Last Post: 19 September 2014 - 10:44 AM Rate Topic: -----

#1 totito88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 09-September 14

How can Sort 5 numbers in Java from high to low with if statements?

Posted 16 September 2014 - 01:18 PM

Hi guys, my name is toty and im new at the forum and JAVA as well.
I have assisted only four classes of JAVA so far and im already surfing the code and stuff, so what you are going to read is pretty basic stuff yet.
Im trying to code a program that orders 5 random numbers from high to low with the basic coding i know (im starting to learn theory of methods... so you can imagine)
When i run the code i cant get all 5 numbers ordered but my logic says the code is right although it's pretty confusing.

I know you could code in a simpler way but first i wanna get it as it is right now.
When i debug (on my own way cause i dont know how to actually use it) shows line 72 with yellow color.


public class NuevaCalculadora {

	import java.util.Scanner;
	
	public static void main(String[] args) {

		int num1;
		int num2;
		int num3;
		int num4;
		int num5;
		int a, b, c, d, e;

		System.out.println("Type in 5 numbers which are going to get ordered from high to low ");
		Scanner type = new Scanner(System.in);
		
		System.out.println("Type in first number: ");
		num1 = type.nextInt();
		System.out.println("Type in second number: ");
		num2 = type.nextInt();
		System.out.println("Type in third number: ");
		num3 = type.nextInt();
		System.out.println("Type in fourth number: ");
		num4 = type.nextInt();
		System.out.println("Type in fifth number: ");
		num5 = type.nextInt();
		
		// assigning the 5 numbers entered to 5 different variables 
		
		a = num1;
		b = num2;
		c = num3;
		d = num4;
		e = num5;

		// checking value of num1 against the other fourth values

		if (num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5)
			a = num1;
		if (num1 < num2 && num1 > num3 && num1 > num4 && num1 > num5)
			b = num1;
		if (num1 < num2 && num1 < num3 && num1 > num4 && num1 > num5)
			c = num1;
		if (num1 < num2 && num1 < num3 && num1 < num4 && num1 > num5)
			d = num1;
		if (num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5)
			e = num1;

		// checking value of num2 against the other fourth values

		if (num2 > num1 && num2 > num3 && num2 > num4 && num2 > num5)
			a = num2;
		if (num2 < num1 && num2 > num3 && num2 > num4 && num2 > num5)
			b = num2;
		if (num2 < num1 && num2 < num3 && num2 > num4 && num2 > num5)
			c = num2;
		if (num2 < num1 && num2 < num3 && num2 < num4 && num2 > num5)
			d = num2;
		if (num2 < num1 && num2 < num3 && num2 < num4 && num2 < num5)
			e = num2;

		// checking value of num3 against the other fourth values

		if (num3 > num2 && num3 > num1 && num3 > num4 && num3 > num5)
			a = num3;
		if (num3 < num2 && num3 > num1 && num3 > num4 && num3 > num5)
			b = num3;
		if (num3 < num2 && num3 < num1 && num3 > num4 && num3 > num5)
			c = num3;
		if (num3 < num2 && num3 < num1 && num3 < num4 && num3 > num5)
			d = num3;
		if (num3 < num2 && num3 < num1 && num3 < num4 && num3 < num5)
			e = num3;

		// checking value of num4 against the other fourth values

		if (num4 > num1 && num4 > num2 && num4 > num3 && num4 > num5)
			a = num4;
		if (num4 < num1 && num4 > num2 && num4 > num3 && num4 > num5)
			b = num4;
		if (num4 < num1 && num4 < num2 && num4 > num3 && num4 > num5)
			c = num4;
		if (num4 < num1 && num4 < num2 && num4 < num3 && num4 > num5)
			d = num4;
		if (num4 < num1 && num4 < num2 && num4 < num3 && num4 < num5)
			e = num4;

		// checking value of num5 against the other fourth values

		if (num5 > num1 && num5 > num2 && num5 > num3 && num5 > num4)
			a = num5;
		if (num5 < num1 && num5 > num2 && num5 > num3 && num5 > num4)
			b = num5;
		if (num5 < num1 && num5 < num2 && num5 > num3 && num5 > num4)
			c = num5;
		if (num5 < num1 && num5 < num2 && num5 < num3 && num5 > num4)
			d = num5;
		if (num5 < num1 && num5 < num2 && num5 < num3 && num5 < num4)
			e = num5;

		System.out.print("El orden de mayor a menor es el siguiente: " + " "
				+ a + " " + b + " " + c + " " + d + " " + e);

	}

	} 



ps: hope you could give some tips!!!! thanks in advanced.

Is This A Good Question/Topic? 0
  • +

Replies To: How can Sort 5 numbers in Java from high to low with if statements?

#2 Invoker   User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 16 September 2014 - 06:53 PM

Look at when you import scanner. There's your error. :)

public class NuevaCalculadora {
// import statement placed in wrong area
	import java.util.Scanner;
	
	public static void main(String[] args) {

		int num1;
		int num2;
		int num3;
		int num4;
		int num5;
		int a, b, c, d, e;

		System.out.println("Type in 5 numbers which are going to get ordered from high to low ");
// I'd advise putting this after your main method line. Some guy told me
// it's good practice
		Scanner type = new Scanner(System.in);


This post has been edited by Invoker: 16 September 2014 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 totito88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 09-September 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 06:49 AM

thanks for your replay Invoker, yes the Scanner is placed in a wrong section and should go after package declaration. But that's not the problem cause in my IDE it's placed in the right spot. The program gets compiled and executed properly but there's a logic error which doesn't sort the numbers right.

This post has been edited by andrewsw: 17 September 2014 - 08:43 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 07:29 AM

Right, so what input are you giving it and what's the output?
Was This Post Helpful? 0
  • +
  • -

#5 Invoker   User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 07:29 AM

That's really odd. I copied your exact code and placed it in my IDE and everything works fine. Not sure what's wrong; could you post the exact error copied from the console.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 07:49 AM

Try different orders of input. It sometimes duplicates numbers and writes over others.
My guess is that the problem is just a typo.

Now, arrays and loops would be the normal approach to this problem but using just ifs presents an interesting challenge.
Was This Post Helpful? 1
  • +
  • -

#7 Invoker   User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 08:02 AM

Yea, I've tried a couple of different inputs, but everything seems to be working. Not near my computer now, so I can't really run test. And yea, I agree this is an interesting way to approach this problem.
Was This Post Helpful? 0
  • +
  • -

#8 totito88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 09-September 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 08:41 AM

This is the final output when running the program:

Type in 5 numbers which are going to get ordered from high to low
Type in first number:
98
Type in second number:
158
Type in third number:
23
Type in fourth number:
36
Type in fifth number:
874
And the order goes: 874 158 23 36 23

Where's the 98? Is 23 higher than 36? :)
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11067
  • View blog
  • Posts: 18,901
  • Joined: 19-March 11

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 09:16 AM

View Postcfoley, on 17 September 2014 - 09:49 AM, said:

Now, arrays and loops would be the normal approach to this problem but using just ifs presents an interesting challenge.


No, not an interesting one.

Arrays, loops, and methods. If you're not familiar with these ideas, complete this tutorial and then this one. When I say "complete", I don't just mean "pass your eyes over the words". I mean take each code example, plug it into some dummy class on your machine, and make it work, then play with it and make it work differently until you know what it does and what you can make it do.

Once you've done this, test your understanding by writing a simple method to exchange the position of two elements in an array, identified by index. The method signature will look like this:

private void swap(int[] arr, int i, int j)
{
  //your code here
}



The inputs are an array of ints and indices i and j. The method changes arr in place, such that
arr = {a,b,c, ..., i, ... p, r, s, ... j ... x, y, z}



becomes

arr = {a,b,c, ..., j, ... p, r, s, ... i ... x, y, z}



Since arrays are objects and live on the heap, changes made to them in a method will persist. (why is this the case? you should wonder about this, and maybe even try to find out the answer)

Then write another method to check whether an array is sorted.

private boolean isSorted(int[] array) {
  // your code here
}


Once you have the swap method, it's easy to write a simple bubblesort - bubblesort is the most basic sort routine imaginable. Look it up, then implement it. Since you have a method that checks whether the array is sorted, you can test whether your implementation works.
Was This Post Helpful? 1
  • +
  • -

#10 Invoker   User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 165
  • Joined: 29-April 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 09:29 AM

Alright, I looked at your code and I can figure out why it isn't working; it isn't long enough and doesn't check every possible scenario.

In my case I entered the values 10, 4, 90, 6, 78.

Now look at your code where you attempt to assign 10 to a variable:

       // First case works, but 10 is not greater than 90 so it doesn't work
       if (num1 > num2 && num1 > num3 && num1 > num4 && num1 > num5)
           a = num1;
       // 10 isn't less than 4
       if (num1 < num2 && num1 > num3 && num1 > num4 && num1 > num5)
           b = num1;
       // same as above
       if (num1 < num2 && num1 < num3 && num1 > num4 && num1 > num5)
            c = num1;
       // same as above
       if (num1 < num2 && num1 < num3 && num1 < num4 && num1 > num5)
            d = num1;

       if (num1 < num2 && num1 < num3 && num1 < num4 && num1 < num5)
            e = num1;



None of these conditions are ever met when the second value is greater than the first (maybe even in more cases than this). Honestly, if-else statements wouldn't be my first choice on solving this. Maybe someone with more experience could pull it off, but, as of now, I'm - more or less - stumped on how this would work. It seems like it would take way more effort than it should.

(Someone correct me if I'm wrong please)

EDIT: If you know how to work with nested if statements, I think you could pull this off; the workload would still increase.

This post has been edited by Invoker: 17 September 2014 - 09:33 AM

Was This Post Helpful? 1
  • +
  • -

#11 totito88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 09-September 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 09:34 AM

Great jon.kiparsky I will be checking out those tutorials as soon as i get home from work!!!
I'll try once I read the tutorials to re-code the program with the new information learned. In case it wont work I'll be posting here again!!!!

Thank you so much!!!
Was This Post Helpful? 0
  • +
  • -

#12 totito88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 09-September 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 09:40 AM

I got it Invoker I'll be rechecking the code to see if I can user IF statements in other way while reading the jon.kiparsky tutorials!!!!
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky   User is online

  • Beginner
  • member icon


Reputation: 11067
  • View blog
  • Posts: 18,901
  • Joined: 19-March 11

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 10:02 AM

View PostInvoker, on 17 September 2014 - 11:29 AM, said:

None of these conditions are ever met when the second value is greater than the first (maybe even in more cases than this). Honestly, if-else statements wouldn't be my first choice on solving this. Maybe someone with more experience could pull it off, but, as of now, I'm - more or less - stumped on how this would work. It seems like it would take way more effort than it should.

(Someone correct me if I'm wrong please)

EDIT: If you know how to work with nested if statements, I think you could pull this off; the workload would still increase.



The workload will be intolerable. In the simplest if-only implementation, you'd do the following:

inputs: ints i0, i1, i2, i3, i4: the initial not-an-array
output: String ""+i0 +" " +i1+" " +i2+" " +i3+" " +i4  (values after sorting)
0. initialize ints f0..f4 to take final values 0,1,2,3,4. Each one gets the initial value of Integer.MAX_VALUE. int tmp to hold swaps
1. Find the final value for i0:
   check to see if i0 is less than i1. if so, assign i1 to tmp, i0 to i1, tmp to i0 
   check to see if i2 is less than i0. if so, swap as above
   same for i3 < i0
   same for i4 < i0
   # i0 is now set. It will not change
2. Find the final value for i1:
  same process as before - compare to i2, i3, i4
   #i1 is now set. It will not change
3. Find the final value for i2
4.  Find the final value for i3
5.  Find the final value for i4 # nothing to do here, i4 will be correct
6. output some string composed of these five values.
7. think about what you'd need to do to make this sort six numbers. 
8. pray that nobody ever asks you to do this  


I think this is correct. By my reckoning, it's ~ five lines per check-and-swap, and there are 4+3+2+1=10 of those. 50 lines? How's that intolerable? It's intolerable because those 50 lines can never do anything but sort 5 integers, and they will never be able to do anything but output those as a String, and because if you ever repeat the same thing three times and you don't make a function out of it or do it in a loop (or some combination of the two), you're doing it wrong. Programming is about finding abstractions. Let the machine do the grunt work - your job is to do the smart.
Was This Post Helpful? 0
  • +
  • -

#14 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 10:43 AM

You're right of course. And it's not interesting for me or you. But the journey is interesting for a beginner, and sometimes you have to feel the pain of doing it the wrong way to appreciate doing it the right way. As it happens, I just coded it up and by coincidence chose the same algorithm as you. (40 lines unless you count the braces btw).

But while we are committing the atrocity of sorting without a loop let's add the minor sin of putting a few commands on the same line.

		int x;
		if (a < B)/> {x = a; a = b; b = x;}
		if (a < c) {x = a; a = c; c = x;}
		if (a < d) {x = a; a = d; d = x;}
		if (a < e) {x = a; a = e; e = x;}
		
		if (b < c) {x = b; b = c; c = x;}
		if (b < d) {x = b; b = d; d = x;}
		if (b < e) {x = b; b = e; e = x;}
		
		if (c < d) {x = c; c = d; d = x;}
		if (c < e) {x = c; c = e; e = x;}
		
		if (d < e) {x = d; d = e; e = x;}



What we can see here is a lot of patterns. First of all, it's like we are counting. I can see sequences like abcd, bcd, etc. If we then teach the beginner about arrays those letters can be replaced with numbers:

		int x;
		if (ns[0] < ns[1]) {x = ns[0]; ns[0] = ns[1]; ns[1] = x;}
		if (ns[0] < ns[2]) {x = ns[0]; ns[0] = ns[2]; ns[2] = x;}
		if (ns[0] < ns[3]) {x = ns[0]; ns[0] = ns[3]; ns[3] = x;}
		if (ns[0] < ns[4]) {x = ns[0]; ns[0] = ns[4]; ns[4] = x;}
		
		if (ns[1] < ns[2]) {x = ns[1]; ns[1] = ns[2]; ns[2] = x;}
		if (ns[1] < ns[3]) {x = ns[1]; ns[1] = ns[3]; ns[3] = x;}
		if (ns[1] < ns[4]) {x = ns[1]; ns[1] = ns[4]; ns[4] = x;}
		
		if (ns[2] < ns[3]) {x = ns[2]; ns[2] = ns[3]; ns[3] = x;}
		if (ns[2] < ns[4]) {x = ns[2]; ns[2] = ns[4]; ns[4] = x;}
		
		if (ns[3] < ns[4]) {x = ns[3]; ns[3] = ns[4]; ns[4] = x;}


Initially it looks like we made it worse but since we are working with arrays we can extract a swap method. This is either a lightbulb moment or a source of confusion. If it's the latter then having the loops unrolled like this might make it easier to explain.

	static void sort() {
		if (ns[0] < ns[1]) {swap(0, 1);}
		if (ns[0] < ns[2]) {swap(0, 2);}
		if (ns[0] < ns[3]) {swap(0, 3);}
		if (ns[0] < ns[4]) {swap(0, 4);}
		
		if (ns[1] < ns[2]) {swap(1, 2);}
		if (ns[1] < ns[3]) {swap(1, 3);}
		if (ns[1] < ns[4]) {swap(1, 4);}
		
		if (ns[2] < ns[3]) {swap(2, 3);}
		if (ns[2] < ns[4]) {swap(2, 4);}
		
		if (ns[3] < ns[4]) {swap(3, 4);}
	}
	
	static void swap(int indexA, int indexB) {
		int x = ns[indexA]; 
		ns[indexA] = ns[indexB]; 
		ns[indexB] = x;
	}



Now it can be reasoned into four loops so easily:

	static void sort() {
		for(int j = 1; j < ns.length; j++) {
			if (ns[0] < ns[j]) {swap(0, j);}
		}

		for(int j = 2; j < ns.length; j++) {
			if (ns[1] < ns[j]) {swap(1, j);}
		}

		for(int j = 3; j < ns.length; j++) {
			if (ns[2] < ns[j]) {swap(2, j);}
		}
		
		for(int j = 4; j < ns.length; j++) {
			if (ns[3] < ns[j]) {swap(3, j);}
		}
	}



The final loop can be a bit tricky for beginners to get their heads around. You could extract a method first which would make it more obvious but maybe a nested loop is a valuable thing to show.

	static void sort() {
		for(int i = 0; i < ns.length; i++){
			for(int j = i + 1; j < ns.length; j++) {
				if (ns[i] < ns[j]) {swap(i, j);}
			}
		}
	}

	static void swap(int indexA, int indexB) {
		int x = ns[indexA]; 
		ns[indexA] = ns[indexB]; 
		ns[indexB] = x;
	}



I think this process is far from boring. It's certainly not trivial from a beginner's perspective. 5 is a nice number. It's short enough to be quick and easy to implement but long enough to make the pain of longer lists obvious.

It also shares a lot of similarities with iterative design in larger programs. How often have you seen two bits of code that are almost the same or do almost the same thing. You tweak and twist them to be actually identical so you can combine them or you separate the common bits from the unique bits. Without knowing it, a beginner who is taken through a process like this will be building up an awareness of more advanced techniques.
Was This Post Helpful? 1
  • +
  • -

#15 totito88   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 7
  • Joined: 09-September 14

Re: How can Sort 5 numbers in Java from high to low with if statements?

Posted 17 September 2014 - 11:24 AM

jon.kiparsky and cfoley your last two post confused me a lot. Looks like Chinese to me eyes hahaha!!!!

ps: Started reading jon's tutorials which are pretty clear!!!
ps2: hope some day could code like your last posts :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2