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

• 10-04-2013, 11:40 PM
Miike
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
• 10-05-2013, 02:23 AM
jim829
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
• 10-05-2013, 06:40 AM
AndrewM16921
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().

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(); }```