# Thread: Sorting Three Integers Using JOptionPane

1. Member Join Date
Jun 2011
Posts
6
Rep Power
0

## Sorting Three Integers Using JOptionPane

Hello
I Need To Write A Java Code That Gets 3 Integers From The User And Sorts Them From Smallest To Largest And Displays In A JOptionPane Dialog Box. The Code I Have So Far Compiles Fine And Runs Great If I Enter 5, 10, 15 But If I Enter 15, 5, 10 It Returns "The Numbers Arranged From Samllest To Largest Are: 5 0 0

Here Is My Code

Java Code:
```import javax.swing.JOptionPane;
public class SortInt1
{

public static void main(String[] args) {

int num1=0, num2=0, num3=0;
int smallest=0, middle=0, largest=0;

for (int i=1; i<4; i++) {
String stringNum = JOptionPane.showInputDialog(null,
"Enter integer number " + i + " to be"
+ " evaluated." );

if (i==1)
num1=Integer.parseInt(stringNum);
else if(i == 2)
num2=Integer.parseInt(stringNum);
else if(i == 3)
num3=Integer.parseInt(stringNum);

}

if ((num1<=num2) && (num1<=num3)){
smallest = num1;
if (num2<=num3){
middle=num2;
largest=num3;}
else {
largest=num2;
middle=num3;}
}

else if((num2 <= num1) && (num2 <= num3)){
smallest=num2;
if (num1<=num3){
middle=num1;
largest=num3;}
}

else if((num3 <= num1) && (num3 <= num2)){
smallest=num3;
if (num2<=num3){
middle=num2;
largest=num1;}
}

JOptionPane.showMessageDialog(null, "The numbers arranged "
+ "from smallest to largest are: "
+ smallest + " " + middle + " " +
largest + ".");

}

}```  Reply With Quote

2. ## You quit before you were finished. Your first major if block has an important else block in it:

Java Code:
```      if ((num1 <= num2) && (num1 <= num3)) {
smallest = num1;
if (num2 <= num3) {
middle = num2;
largest = num3;
} [color="red"][b]else[/b][/color] {
largest = num2;
middle = num3;
}
}```

Where's the similar else block in your three other major if blocks?  Reply With Quote

3. Moderator  Join Date
Jul 2010
Location
California
Posts
1,638
Rep Power
12

## Step through the logic of your code slowly for the input...make note that you have some if conditions that don't have else code, and may result in the middle and largest values never to be set (eg they will be 0)

Edit: too slow again....ditto what Fubarable said too  Reply With Quote

4. ## Sounds like you need to check your logic to see why that is happening. One way to do that is to add printlns to output the values of variables and the results of each comparison. The printed output can show you where the code is not doing what you want it to do.
For example print out the values of num1, num2 and num3 after the loop where they are read in to be sure that they are all read in ok.

You should play computer with the code. Take a piece of paper and write down the values for the three numbers and then go thru your logic and see if the correct values are chosen.

Your comparision logic might be wrong. There is a better way to search for the number, but if you don't know how to use arrays then that technique will have to wait until your learn about arrays.  Reply With Quote

5. Member Join Date
Jun 2011
Posts
6
Rep Power
0

## Thank You All So Very Much And I Apologize I Am Trying To Teach Myself Java And Didn't Realize I Had Forgotten My Else Statement. Thank You Fubarable
I Added And Adjusted The Else Statement And Now It Works Perfectly. I Appreciate The Quick Replies And You All Being Extremely Helpful.  Reply With Quote

6. ## Just because it is recommended to use camel case in Java doesn't mean you have to use it in your posts.  Reply With Quote

7. ## Yup. That post deserves capital punishment.

db  Reply With Quote

8. ##  Originally Posted by DarrylBurke Yup. That post deserves capital punishment.
Ohhh, my liver hurts! Oh the pain!!!  Reply With Quote

integers, java, sorting 