1

I'm currently in my first semester. I have a project requiring me to build a program having a user input 3 words, sort them alphabetically and output the middle word. I have done some searching and seem to only come back with results for sorting 2 words. I so far have code to get the user input but I am completely lost as to how to sort them alphabetically and how to prompt the user to enter the three strings. Please be patient with me as I am very new to programming. If anyone can provide me with any advice or the best or easiest way to go about sorting these I would greatly appreciate it

import java.util.Scanner; //The Scanner is in the java.util package.

public class MiddleString {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //Create a Scanner object.

String str1, str2, str3;

System.out.println("Please enter one word words : "); //Prompt user to enter one word
str1=input.next(); //Sets "str1" = to first word.
str2=input.next(); //Sets "str2" = to second word.
str3=input.next(); //Sets "str3" = to third word.



System.out.println("The middle word is " ); // Outputs the middle word in alphabetical order.

}
}

Please help!

user3236405
  • 11
  • 1
  • 2
  • 1
    Hint: `String` objects have a [`compareTo()`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo%28java.lang.String%29) method. – Undefined Feb 08 '14 at 20:38
  • *Must* you use separate variables? Ideally a List (or even array) would be used to collect such input (as collections are [trivially sorted](http://stackoverflow.com/questions/708698/how-to-sort-list-alphabetically)). – user2864740 Feb 08 '14 at 20:39

2 Answers2

1

Try something like this:

String [] strings;    
int i = 0;    
System.out.println("Please enter one word words : "); //Prompt user to enter one word   
strings[i++] = input.next(); //Sets "str1" = to first word.  
strings[i++] = input.next(); //Sets "str2" = to second word.  
strings[i++] = input.next(); //Sets "str3" = to third word.  
Arrays.sort(strings);  
System.out.println("The middle word is " + strings[strings.length / 2]);  
Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • I assumed (perhaps incorrectly) that since the OP is in his/her first semester and new to programming, that the instructor wanted them to complete this assignment _without_ arrays. – Undefined Feb 08 '14 at 20:44
  • @Undefined:You might be right. But on the other hand isn't arrays (as a fundamental data type) expected to be taught *before* getting user input? – Cratylus Feb 08 '14 at 20:48
  • Not where I go to school. – Undefined Feb 08 '14 at 20:49
0

You can sort (compare) only two words at a time, yes, but that is the basis for the whole sorting algorithm. You'll need to loop through your array of words and compare each word with each other word.

String[2] words = new String[2]; 
words[0] = input.next();
words[1] = input.next(); 
words[2] = input.next();

String[2] sortedWords = new String[2];

for (String word: words){ // outer loop
    for (String word: words){ // inner loop to compare each word with each other
        // logic to do the comparisons and sorting goes here
    } 
}

System.out.println(sortedWords[1]);

Of course I've left out the fun part for you, but that should get you started.

Michael
  • 455
  • 7
  • 18