Results 1 to 8 of 8
  1. #1
    luckyirish is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default 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

    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 + ".");
    
       }
    
    
    }
    Thank You For Your Help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    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.

  5. #5
    luckyirish is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

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

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Yup. That post deserves capital punishment.

    db

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. how do you add up integers in an array?
    By shazakala in forum New To Java
    Replies: 7
    Last Post: 04-19-2011, 10:32 AM
  2. Sorting 4 Integers using only If statements
    By Leirsgrios in forum New To Java
    Replies: 10
    Last Post: 02-02-2011, 05:03 AM
  3. Sum of Integers - From File
    By Cubba27 in forum New To Java
    Replies: 8
    Last Post: 03-25-2010, 11:45 AM
  4. Sorting 3 Integers Using If Else
    By MSteinman in forum New To Java
    Replies: 12
    Last Post: 02-19-2010, 12:52 PM
  5. Set of Integers
    By rsjava24 in forum New To Java
    Replies: 7
    Last Post: 01-28-2010, 10:29 AM

Tags for this Thread

Posting Permissions

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