Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: how to sort names using compareto

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default how to sort names using compareto

    i have this problem with my code.

    it need to put three names in alphabetical order that are entered from the user. then output the alphabetical result. the program compiles but when you put in different names there are not alphabeticals. i think only the first if works good. any help would be appreciated.

    import javax.swing.JOptionPane;
     
    public class Sort
    {
    	public static void main(String[] args) 
    	{
    		String name1;
    		String name2;
    		String name3;
     
    		name1 = JOptionPane.showInputDialog(null, "Enter a name");
    		name2 = JOptionPane.showInputDialog(null, "Enter second name");
    		name3 = JOptionPane.showInputDialog(null, "Enter third name");
     
    		if (name1.compareTo(name2) < 0)
    		{
    			if (name2.compareTo(name3) < 0)
    		 	{
    		 		JOptionPane.showMessageDialog(null, "Alphabetical order: \n" 
    		 						+ name1 + "," + name2 + "," + name3);
    		 	}
    		 	else
    		 	{
    		 		JOptionPane.showMessageDialog(null, "Alphabetical order: \n" 
    		 						+ name1 + "," + name3 + "," + name2);
    		 	}
     
     
    		}
     
    		else if (name2.compareTo(name1) < 0)
    		{
    		 	if (name1.compareTo(name3) < 0)
    		 	{
    		 		JOptionPane.showMessageDialog(null, "Alphabetical order: \n" 
    		 						+ name2 + "," + name1 + "," + name3);
    		 	}
    		 	else
    		 	{
    		 		JOptionPane.showMessageDialog(null, "Alphabetical order: \n" 
    		 						+ name2 + "," + name3 + "," + name1);
    		 	}
     
    		}
     
     
    		else if (name3.compareTo(name1) < 0)
    		{
    		 	if (name1.compareTo(name2) < 0)
    		 	{
    		 		JOptionPane.showMessageDialog(null, "Alphabetical order: \n" 
    		 						+ name3 + "," + name1 + "," + name2);
    		 	}
    		 	else
    		 	{
    		 		JOptionPane.showMessageDialog(null, "Alphabetical order: \n" 
    		 						+ name3 + "," + name2 + ","+ name1);
    		 	}
     
    		}
     
    	}
    }


  2. #2
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: how to sort names using compareto

    i would add names to array compare index 1 with 0 and ind index 2 with 1(array[i] with array[i-1]) and swap the words if not correctly position
    (bubblesort)

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: how to sort names using compareto

    i can only use compareto as that is where my class is currently are and im a beginner. i cant seem to find the error in my if statements any help

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: how to sort names using compareto

    Quote Originally Posted by kittykat0953 View Post
    i cant seem to find the error in my if statements any help
    The error is in the concept of ifs structure.

    Get for example the first block, which conceptually (pseudo code) means:
    IF name1 < name2 THEN
        IF name2 < name3 THEN
            sequence is name1 , name2 , name3
        ELSE
            sequence is name1 , name3 , name2
        ENDIF
    ENDIF

    Given:
    name1="B"
    name2="F"
    name3="A"

    the first IF is true. The second IF is false. But this does not mean that the sequence is name1 , name3 , name2 (B A F)!

    You have to restructure and add if statements.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. compareTo() question
    By Hikaros in forum Java Theory & Questions
    Replies: 5
    Last Post: September 9th, 2013, 12:34 AM
  2. Replies: 7
    Last Post: November 18th, 2012, 05:17 AM
  3. How to call a C sort function to sort a Java Array.
    By Dwere13 in forum Java Native Interface
    Replies: 22
    Last Post: July 12th, 2012, 04:44 PM
  4. Help with using mergesort to sort a list of names alphabetically?
    By BearBearBear in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 27th, 2012, 09:10 AM
  5. Help Me with my Program CompareTo!!!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 7th, 2011, 12:54 PM