# Thread: input 3 numbers output order least to greatest

1. Member
Join Date
Nov 2010
Posts
37
Rep Power
0

## 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.
Java 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);
}```
Last edited by rizowski; 01-14-2011 at 03:53 AM.

2. 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.

3. 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.

4. Moderator
Join Date
Jul 2010
Location
California
Posts
1,641
Rep Power
9
Look carefully at the conditionals for that input (none will ever be met). What exactly is the goal? Sorting?

5. 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.
Last edited by sunde887; 01-14-2011 at 04:38 AM.

6. Member
Join Date
Nov 2010
Posts
37
Rep Power
0

## 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)
Java Code:
```public static void main(String[] args) {
int [] arra;
arra = new int[3];

Scanner scan = new Scanner(System.in);
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
Last edited by rizowski; 01-14-2011 at 08:41 AM.

7. Originally Posted by rizowski
Java 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,
Java 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

8. with the change goldest suggested you can also allow for a larger amount of numbers to be used. if you print with
Java Code:
`incr < arra.length`
You can also start the program with
Java Code:
```System.out.println("Please enter how many numbers:");
int max = scan.nextInt();
arra = new int[max];```
Since you are using the
Java 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

9. If you want to avoid all those ugly if-statements for finding the order of those three numbers, have a look at this:

Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•