Results 1 to 6 of 6
  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Sorting Help Needed for a Beginner

    Hi.
    I wrote a program that asks the user for 3 names. After entering all of the names in a different dialogue box, sort them, and then show the sorted list in a dialog box.
    I don't know how to sort. I don't know what to do or how to start.
    Help.:confused:

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    How would you do it by hand, without any programming? Make up three names and sort them. Write down exactly what you're doing while being as specific as possible. Pretend you're writing directions for somebody who has no idea how to sort a list of names. After you have those directions, it should be pretty easy to translate to code.

  3. #3
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default

    This is my output statement but it is not showing any data.

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

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    You haven't answered my question about how you'd do this "by hand" yet. You haven't shown any code (if you do, make sure it's in SSCCE form). How do you expect to get help?

    Recommended reading: How To Ask Questions The Smart Way

  5. #5
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default Sorting Help Needed for a Beginner

    Kevin I'm not sure if this is what you are asking for. If I was sorting the names Abe, Moses, Delilah "manually" I would just rearrange them to be in alphabetical order.
    Abe
    Delilah
    Moses.
    I apologize if I am not understanding your question.

    This is my code:
    Java Code:
    /* Declare String variables */ 
    	    String name1;
    	    String name2;
    	    String name3;
    	    String orderStr;
    		 
    		/* Set-up variables and Dialog boxes */
    		name1 = JOptionPane.showInputDialog
    		   ("This program asks the user to enter 3 names\n"
    			+"The names will then be displayed in alphabeticalorder\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 = " ";
    		  
    		  
    		  /* Make String comparisons and sort */
    		  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;
    				
    				
    			/* Show the output of the data in alphabetical order */
    			JOptionPane.showMessageDialog(null,"The names in alphabetical order: "
    		      + orderStr,"Alphabetizing complete",
    				JOptionPane.INFORMATION_MESSAGE);
    				
    			
    
    
    		   
    
       }
    }

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

Similar Threads

  1. Beginner Q: help needed
    By smallmos1 in forum New To Java
    Replies: 4
    Last Post: 10-05-2010, 06:35 AM
  2. Help Needed.. Java Beginner
    By levis lover in forum New To Java
    Replies: 5
    Last Post: 06-17-2010, 06:29 AM
  3. Beginner, need a little help
    By jimmy-lin in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:00 AM
  4. beginner here...help please
    By shroomiin in forum New To Java
    Replies: 6
    Last Post: 09-15-2009, 11:06 PM
  5. almost done...beginner needs help plz..
    By shongo in forum New To Java
    Replies: 15
    Last Post: 11-10-2008, 08:14 AM

Posting Permissions

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