Results 1 to 3 of 3
  1. #1
    Miike is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Unhappy Program asking the user to imput 3 names and print them out in alphabetical order

    Quote for Quote from the text: "Write a program that asks the user to enter three names, and then displays the names sorted in ascending order. For example, if the user entered "Charlie","Leslie, and "Andy", the program would display

    Andy
    Charlie
    Leslie

    My professor specifically said we are not allowed to use loops or arrays since we have not covered that in class. I have been trying to use the compareTo method but cant seem to get it to run with more then 2 string variables

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: Program asking the user to imput 3 names and print them out in alphabetical order

    Think comparisons based on comparisons. And try to work it out on paper first.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Program asking the user to imput 3 names and print them out in alphabetical order

    Instead of trying to use more than 2 Strings with compareTo, compare it multiple times. I'll write a similar example using integers, and you can base your String ordering off of that. Though, in the future (when you're not restricted by the rules of the assignment) you should use Arrays.sort() or Collections.sort().

    Java Code:
    public static void main(String args[])
    {
    	sort(1, 2, 3);
    	sort(3, 2, 1);
    	sort(2, 3, 1);
    	sort(1, 3, 2);
    	sort(1, 3, 1);
    }
    
    public static void sort(int a, int b, int c)
    {
    	int first, second, third;
    	if(a < b)
    	{
    		if(a < c)
    		{
    			first = a;
    			if(b < c)
    			{
    				second = b;
    				third = c;
    			}
    			else
    			{
    				second = c;
    				third = b;
    			}
    		}
    		else
    		{
    			first = c;
    			second = a;
    			third = b;
    		}
    	}
    	else
    	{
    		if(b < c)
    		{
    			first = b;
    			if(a < c)
    			{
    				second = a;
    				third = c;
    			}
    			else
    			{
    				second = c;
    				third = a;
    			}
    		}
    		else
    		{
    			first = c;
    			second = b;
    			third = a;
    		}
    	}
    	
    	System.out.println(first);
    	System.out.println(second);
    	System.out.println(third);
    	System.out.println();
    }

Similar Threads

  1. How to print array from user input?
    By sims34 in forum New To Java
    Replies: 1
    Last Post: 01-29-2013, 04:46 AM
  2. Replies: 1
    Last Post: 03-25-2012, 09:25 PM
  3. array to sort names in alphabetical order
    By leoshiner in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 12:28 PM
  4. [SOLVED] imput data from HTML till an Applet?
    By AlejandroPe in forum New To Java
    Replies: 0
    Last Post: 03-31-2009, 09:47 AM
  5. How to sort array objects into alphabetical order...
    By lisalala in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 11:01 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
  •