input 3 numbers output order least to greatest

• 01-14-2011, 03:51 AM
rizowski
input 3 numbers output order least to greatest
Can someone please explain why when i put in 2,1,3 it doesn't exicute anything? To me it looks right, the debug doesn't stop, it just keeps going. it Should output 1,2,3.
Code:

```if ((num1<num2) && (num2<num3) && num3>num1)                         {       System.out.println(num1+" "+num2+" "+num3);                         } if ((num3<num1) && (num1<num2)&& num2 > num3)                         {       System.out.println(num3+" "+num1+" "+num2);                     } if ((num2<num3) && (num3<num1) && num1>num2)                         {         System.out.println(num2+" "+num3+" "+num1);                         }```
• 01-14-2011, 04:06 AM
Junky
Why don't you simply replace your variables with the corresponding values?

For input 2 1 3
num1 = 2
num2 = 1
num3 =3

First if:
if ((num1<num2)
if 2 < 1
false

Second if:
if ((num3<num1)
if 3 < 2
false

Third if:
if num3<num1
Aleady established above it is false.

Therefore all if statements are false so your code does nothing.

This is better achieved with nested if/else statements. Or even better place values in an array or List and sort them.
• 01-14-2011, 04:10 AM
Junky
Another thing with 3 numbers there are 6 possible combinations, whereas your code only prionts out 3 combinations. So clearly your code is lacking 3 more cases.
• 01-14-2011, 04:10 AM
doWhile
Look carefully at the conditionals for that input (none will ever be met). What exactly is the goal? Sorting?
• 01-14-2011, 04:33 AM
sunde887
like Junky said, create an array, prompt the user for a size, then initialize the array to that size, then prompt for items and store each in the array, finally, you can use a loop to print the array.

However, if you are unfamiliar with array usage, or you want to do it/have to do it with if/else clauses

you need to have every possible combination thought of, first, what if some numbers are equal? You are simply checking if one is less, if the input is 3, 2, 3, what should it do?

Second, you need, like stated previously, 6 combinations for an input length of 3, 3 * 2 * 1 = possible combinations.
now, the combos should be
(1 = item1, 2 = item2, 3 = item3)
1, 2, 3
1, 3, 2
...

make one clause for each of them, include checking <= instead of simply < and you should be able to sort them correctly.
• 01-14-2011, 08:39 AM
rizowski
Thanks :D
Thanks everyone for your posts. I greatly appreciate all of it :D.
I like it when people post their resolution on a board. so I ended up going with an array and wrote this: (I was aiming for a sort function)
Code:

```public static void main(String[] args) {                 int [] arra;                 arra = new int[3];                                 Scanner scan = new Scanner(System.in);                 System.out.println("Please enter three numbers");                 int num1 = scan.nextInt();                 scan.nextLine();                 int num2 = scan.nextInt();                 scan.nextLine();                 int num3 = scan.nextInt();                                 arra[0]=num1;                 arra[1]=num2;                 arra[2]=num3;                                 Arrays.sort(arra);                                 for (int incr = 0; incr < 3; incr++)                         System.out.println(arra[incr]);         }```
Thanks again for everyones response :D
• 01-14-2011, 08:59 AM
goldest
Quote:

Originally Posted by rizowski
Code:

`for (int incr = 0; incr < 3; incr++)`

Don't have habit of using hard coded values for array iterations, use the length property instead. Like,
Code:

`for (int incr = 0; incr < arra.length; incr++)`

There is nothing wrong with your code, but just a small suggestion. A wrong value sometimes can lead to ArrayIndexOutOfBoundsException.

Goldest
• 01-14-2011, 09:36 AM
sunde887
with the change goldest suggested you can also allow for a larger amount of numbers to be used. if you print with
Code:

`incr < arra.length`
You can also start the program with
Code:

```System.out.println("Please enter how many numbers:"); int max = scan.nextInt(); arra = new int[max];```
Since you are using the
Code:

`array.sort`
instead of a series of if else clauses it is very simple.

finally, you can have a loop that stores the numbers in an array for you instead of creating three new int variables and then manually entering them all

just have each step in the loop prompt the user and store the item in the i position of the array.

These are really just some things you can think about in the future to make your code more, reusable, I suppose, not really sure what word to use
• 01-14-2011, 10:07 AM
JosAH
If you want to avoid all those ugly if-statements for finding the order of those three numbers, have a look at this:

Code:

```int min= Math.min(num1, Math.min(num2, num3)); int max= Math.max(num1, Math.max(num2, num3)); int mid= num1+num2+num3-min-max;```
Don't turn this in as if it were your homework; your teacher might call for psychiatric help. This was just for fun ;-)

kind regards,

Jos