-1
import java.util.Scanner;

public class alphabetical {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner Alphabet= new Scanner(System.in);
    System.out.println("Input First Name");
    String UserInput= Alphabet.next();
    System.out.println("Input second name");
    String UserInput2= Alphabet.next();
    System.out.println("Input third name");
    String UserInput3= Alphabet.next();
    System.out.println(alpha)UserInput,UserInput2,UserInput3));
}

    public static void alpha(String fromUser,String fromUser2, String fromUser3)
    {
    if (fromUser.compareTo(fromUser2)>0)
    {
        System.out.println(fromUser2);
    }
    else if(fromUser.compareTo(fromUser3)>0)
    {
        System.out.println(fromUser3);
    }
    else if (fromUser2.compareTo(fromUser3)>0)
    {
        System.out.println(fromUser3);
    }
    else if (fromUser2.compareTo(fromUser)>0)
    {
        System.out.println(fromUser);
    }
    else if (fromUser3.compareTo(fromUser)>0)
    {
        System.out.println(fromUser);
    }
    else if (fromUser3.compareTo(fromUser2)>0)
    {
        System.out.println(fromUser2);
    }
    
}

}

So that's my code but I don't know what I'm doing wrong. I've been working on this for a while and I need a code that will allow the user to input 3 names and then sort the names in alphabetical order

The requirements for this program is to have the user input 3 strings and print them out ordered alphabetically using a function that takes 3 strings-- the return type should be void-- this means that there nothing returned back to main, the function will just print out the three words in alphabetical order there should be 6 cases you need to worry about (think If, elseif...else). Here is what a sample output might look like in the console (> denotes it's in the console-- you won't actually see this):

input first lowercase string

awesome

input second lowercase string

bogus

input third lowercase string

chillin

(THE FOLLOWING HAPPENS IN THE VOID FUNCTION)

Here are your words in alphabetical order

awesome

bogus

chillin

Community
  • 1
  • 1

5 Answers5

1

If you're really not allowed to use arrays or lists, I hope your professor is making you write a long-winded solution, so that he can "reveal" the better, array or list based version later.

For three items, it's true that there are six cases, and you can "just" write an if/else clause for each one:

  // case 1 - abc
  if(lessThan(a,b) && lessThan(b,c)) {
     System.out.println(a + " " + b + " " + c);
  }
  // case 2 - acb
  else if(lessThan(a,c) && lessThan(c,b)) {
     System.out.println(a + " " + c + " " + b);
  }
  // case 3 - bac
  else if(lessThan(b,a) && lessThan(a,c)) {
     System.out.println(b + " " + a + " " + c);
  }

... and so on, for each of abc,acb,bac,bca,cab,cba. For my own sanity I've assumed the existence of a lessThan() method containing a.compareTo(b) < 0 -- but you could use compareTo() directly if your professor also forbids you writing helper methods.

Because of the wording of the question, I guess this is what's expected -- it's not a sensible way to implement a sort, but it could be the basis on which to build something better. It does also allow you to directly count how many comparisons are being made, which could lead to some beginner's insight into the cost of an algorithm.


If you're allowed to use an array, and you're allowed to use a sort routine provided by Java, then just put the values into an array, sort it and print it:

  public static void alpha(String a,String b, String c) {
      String[] array = new String[] {a,b,c};
      Arrays.sort(array);
      System.out.println(array[0] + " " + array[1] + " " + array[2]);
  }
slim
  • 40,215
  • 13
  • 94
  • 127
0

So you can use anything as long as you put it in a method ...

public static void alpha(String fromUser,String fromUser2, String fromUser3){
    String[] sArray = {fromUser, fromUser2, fromUser3};
    Arrays.sort(sArray);
    for (String s : sArray){
       System.out.print(s + " ");
    }
 }
Robin Topper
  • 2,295
  • 1
  • 17
  • 25
0

In java 8 it is simple. You can sort collection of strings with lambda:

public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner Alphabet = new Scanner(System.in);
        System.out.println("Input First Name");
        String UserInput = Alphabet.next();
        System.out.println("Input second name");
        String UserInput2 = Alphabet.next();
        System.out.println("Input third name");
        String UserInput3 = Alphabet.next();
        List<String> list = Arrays.asList(UserInput, UserInput2, UserInput3);
        list.sort((String in1, String in2) -> in1.compareTo(in2));
        list.forEach(System.out::println);

    }
Jay Smith
  • 2,331
  • 3
  • 16
  • 27
0

You're close, but you need to be comparing your String to both other Strings before producing your output. One approach is to create an individual if/else if/else block for each of your three lines of output. For example, here's the middle line given Strings s1, s2, s3.

// First Word
. . .
// Second Word
if ( (s1.compareTo(s2) >= 0) && (s1.compareTo(s3) <= 0) ) {
    System.out.println(s1);
} else if ( (s2.compareTo(s1) >= 0) && (s2.compareTo(s3) <= 0) ) {
    System.out.println(s2);
} else {
    System.out.println(s3);
}
// Third Word
. . .

Another approach is to account for every permutation of how three things can be arranged. {abc, acb, bac, bca, cab, cba }. This is manageable when you have only three things to arrange, but gets quite nasty when you add more.

if ( (s1.compareTo(s2)<=0) && (s1.compareTo(s3)<=0) && (s2.compareTo(s3)<=0) ) {
    // abc
    System.out.println(s1);
    System.out.println(s2);
    System.out.println(s3);
} else if (s1.compareTo(s2)<=0) && (s1.compareTo(s3)<=0) && (s2.compareTo(s3)>=0) {
    // acb
    System.out.println(s1);
    System.out.println(s3);
    System.out.println(s2);
} else if . . .
phatfingers
  • 9,770
  • 3
  • 30
  • 44
0

Using java 8 streams you can do it like that:

public static void alpha(String fromUser,String fromUser2, String fromUser3) {
    Stream.of(fromUser, fromUser2, fromUser3).sorted().forEach(System.out::println);
}
Dawid Kunert
  • 183
  • 2
  • 11