Welcome to Dream.In.Code
Become a Java Expert!

Join 149,499 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,380 people online right now. Registration is fast and FREE... Join Now!




Alphabetizing using compound and nested if statements

 
Reply to this topicStart new topic

Alphabetizing using compound and nested if statements

lincolnpark583
21 Mar, 2007 - 08:05 PM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 11


My Contributions
Ok, I'm having trouble understanding what I'm doing wrong with my code. I'm having trouble understanding how the if statement works because at this point it just seems to be reading the first "if" statement and executes that order for the names regardless of what names I type. This program is supposed to re-display the three entered names in alphabetical order. Some input on what I'm doing wrong would be greatly appreciated! Thanks!

CODE
import javax.swing.JOptionPane;


public class NameSort {

  public static void main(String[] args)
  {
    String name1;
    String name2;
    String name3;
    String orderStr;
    


    name1 = JOptionPane.showInputDialog
        ("This program asks the user to enter 3 names\n"
         +"The names will then be displayed in alphabetical order\n\n\n"
         +"Please enter the first name");

    name2 = JOptionPane.showInputDialog
        ("Please enter the second name");

    name3 = JOptionPane.showInputDialog
        ("Please enter the third name");

    orderStr = "";
    
    if (name1.compareTo(name2) >0)
        orderStr = name1 +" " + name2+" " +name3;
    else if (name1.compareTo(name3) >0)
        orderStr = name3 +" " + name2+" " +name1;
    else if (name2.compareTo(name3) >0)
        orderStr = name1 +" " + name2+" " +name3;
    else if (name2.compareTo(name1) >0)
        orderStr = name1+" " + name2+" " +name3;
    else if (name3.compareTo(name1) >0)
        orderStr = name1 +" " + name2+" " +name3;
    else if(name3.compareTo(name2) >0)
        orderStr = name1 +" " + name2+" " +name3;


    

    JOptionPane.showMessageDialog(null,"The alphabetical order "
                                    + "of the names is:" + orderStr,
                                    "Alphabetizing complete",
                                    JOptionPane.INFORMATION_MESSAGE);

      System.exit(0);


  }
}

User is offlineProfile CardPM
+Quote Post

keems21
RE: Alphabetizing Using Compound And Nested If Statements
21 Mar, 2007 - 09:29 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
Try looking at your if statements like this:
CODE

if (name1.compareTo(name2) >0)
        orderStr = name1 +" " + name2+" " +name3;
    else
        if (name1.compareTo(name3) >0)
            orderStr = name3 +" " + name2+" " +name1;
        else
            if (name2.compareTo(name3) >0)
                orderStr = name1 +" " + name2+" " +name3;
            else
                if (name2.compareTo(name1) >0)
                    orderStr = name1+" " + name2+" " +name3;
                else
                    if (name3.compareTo(name1) >0)
                        orderStr = name1 +" " + name2+" " +name3;
                    else
                        if(name3.compareTo(name2) >0)
                            orderStr = name1 +" " + name2+" " +name3;

What an if statement does is check for a conditiona that is either determined to be true or fales. If the condition is true, then the code under the if statement is executed, if it is false, then it is not. Nested if statements are special in that they can be used to check cases with increasing detail. So, for example, if the first statement is false, the code under the else statement will be executed (which happens to be another if statement).

Your problem here is that if the first if statement is true, then none of the other code is ever looked at. To prove this, enter the following names into your program:
CODE

Zeb
Anthony
Mark


These names will not be printed out in the same order that they were inputted, but they may still not be right.

What you need to do, is to be able to check all three names in one if statement instead of only two.
One way to do this would be to use the && operator.
For instance:
CODE

if(name1.compareTo(name2) > 0 && name2.compareTo(name3) > 0)
    //then set your string to whatever you want


See what you can do with this. Good luck.
User is offlineProfile CardPM
+Quote Post

lincolnpark583
RE: Alphabetizing Using Compound And Nested If Statements
22 Mar, 2007 - 02:21 AM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 11


My Contributions
Ok, so essentially I need to compare three conditions to get the sequence of names correct. So is the coding like this correct or on the right track? Because when I ran the code like this it still wasn't putting it in the correct order.

CODE


if (name1.compareTo(name2) > 0 && name2.compareTo(name3) > 0 && name1.compareTo(name3) > 0)
        orderStr = name1 +" " + name2+" " +name3;
    else
        if (name1.compareTo(name2) >0 && name1.compareTo(name3) > 0 && name3.compareTo(name2) > 0)
            orderStr = name1 +" " + name3+" " +name2;
        else
            if (name2.compareTo(name3) > 0 && name2.compareTo(name1) > 0 && name1.compareTo(name3) > 0)
                orderStr = name2 +" " + name1+" " +name3;
            else
                if (name2.compareTo(name3) > 0 && name2.compareTo(name1) > 0 && name3.compareTo(name1) > 0)
                    orderStr = name2+" " + name3+" " +name1;
                else
                    if (name3.compareTo(name1) > 0 && name3.compareTo(name2) > 0 && name2.compareTo(name1) > 0)
                        orderStr = name3 +" " + name2+" " +name1;
                    else
                        if (name3.compareTo(name1) > 0 && name3.compareTo(name2) > 0 && name1.compareTo(name2) > 0)
                            orderStr = name3 +" " + name1+" " +name2;

User is offlineProfile CardPM
+Quote Post

lincolnpark583
RE: Alphabetizing Using Compound And Nested If Statements
22 Mar, 2007 - 11:10 AM
Post #4

New D.I.C Head
*

Joined: 21 Mar, 2007
Posts: 11


My Contributions
Ok, I figured it out! I had the order of the names comparing them from largest to smallest (so it was putting them into reverse alphabetical order, lol. Thanks so much for your help! I completely forgot about the && operator. This is my first Java class ever, as well as first programming class in general. Seems like a lot of information they're throwing at me to learn. I'm enjoying it though! Here's what the re-written code looks like, for those who may need reference later on...

CODE

if (name1.compareTo(name2) > 0 && name2.compareTo(name3) > 0 && name1.compareTo(name3) > 0)
        orderStr = name3 +" " + name2+" " +name1;
    else
        if (name1.compareTo(name2) >0 && name1.compareTo(name3) > 0 && name3.compareTo(name2) > 0)
            orderStr = name2 +" " + name3+" " +name1;
        else
            if (name2.compareTo(name3) > 0 && name2.compareTo(name1) > 0 && name1.compareTo(name3) > 0)
                orderStr = name3 +" " + name1+" " +name2;
            else
                if (name2.compareTo(name3) > 0 && name2.compareTo(name1) > 0 && name3.compareTo(name1) > 0)
                    orderStr = name1+" " + name3+" " +name2;
                else
                    if (name3.compareTo(name1) > 0 && name3.compareTo(name2) > 0 && name2.compareTo(name1) > 0)
                        orderStr = name1 +" " + name2+" " +name3;
                    else
                        if (name3.compareTo(name1) > 0 && name3.compareTo(name2) > 0 && name1.compareTo(name2) > 0)
                            orderStr = name2 +" " + name1+" " +name3;


User is offlineProfile CardPM
+Quote Post

keems21
RE: Alphabetizing Using Compound And Nested If Statements
23 Mar, 2007 - 09:47 AM
Post #5

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
Hey congrats. I'm glad that you were able to work everything out on your own.

There's no better feeling than figuring something out after staring blankly at a computer screen for what seems like hours.

Keep up the good work.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month