Results 1 to 8 of 8
- 06-22-2011, 01:53 AM #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
Please Help I Don't Know What Is Wrong With My Code.
Here Is My Code
Thank You For Your HelpJava 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 + "."); } }
-
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?
- 06-22-2011, 02:03 AM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
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
- 06-22-2011, 02:03 AM #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.
- 06-22-2011, 02:33 AM #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.
- 06-22-2011, 02:41 AM #6
Just because it is recommended to use camel case in Java doesn't mean you have to use it in your posts.
- 06-22-2011, 07:38 AM #7
Yup. That post deserves capital punishment.
db
-
Similar Threads
-
how do you add up integers in an array?
By shazakala in forum New To JavaReplies: 7Last Post: 04-19-2011, 10:32 AM -
Sorting 4 Integers using only If statements
By Leirsgrios in forum New To JavaReplies: 10Last Post: 02-02-2011, 05:03 AM -
Sum of Integers - From File
By Cubba27 in forum New To JavaReplies: 8Last Post: 03-25-2010, 11:45 AM -
Sorting 3 Integers Using If Else
By MSteinman in forum New To JavaReplies: 12Last Post: 02-19-2010, 12:52 PM -
Set of Integers
By rsjava24 in forum New To JavaReplies: 7Last Post: 01-28-2010, 10:29 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks