Reading three words and sorting them in lexicographic order
        Posted  
        
            by 
                Derrick
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Derrick
        
        
        
        Published on 2013-10-20T02:56:17Z
        Indexed on 
            2013/10/20
            3:54 UTC
        
        
        Read the original article
        Hit count: 290
        
java
I am trying to create a program that asks the User to type three words and sort them in lexicographic order.
EXAMPLE;
- Enter three words separated by spaces:
 - Pear Orange Apple
 - Apple
 - Orange
 - Pear
 
The program is working fine (if I attempt the above example) except for one type of combination example that I will show below.
EXAMPLE;
- Enter three words separated by spaces:
 - Orange Apple Pear
 - Apple
 - Pear
 - Pear
 
The program is skipping the first word (Orange) if it is supposed to appear in the middle of the three words.
I believe that this line of code is affecting the program because it says that "this assigned value is never used" but I'm not sure how to fix it since I'm still an entry Java learner.
- middle = firstWord;
 
Because of that line being unused, it's why Pear appeared twice.
import java.util.*;
public static void main(String[] args) 
{
Scanner wordInput = new Scanner(System.in);
String firstWord;
String secondWord;
String thirdWord;
System.out.println("Enter three words separated by spaces: ");
firstWord = wordInput.next();
secondWord = wordInput.next();
thirdWord = wordInput.next();
String top = firstWord;
String bottom = firstWord;
if( top.compareTo(secondWord) > 0) 
{ 
top = secondWord; 
} 
if( top.compareTo(thirdWord) > 0) 
{ 
top = thirdWord; 
} 
if( bottom.compareTo(secondWord) < 0) 
{ 
bottom = secondWord; 
} 
if( bottom.compareTo(thirdWord) < 0) 
{ 
bottom = thirdWord; 
}   
String middle;
if( !firstWord.equals(bottom) && !firstWord.equals(top) ) 
{ 
middle = firstWord; 
} 
if( !secondWord.equals(bottom) && !secondWord.equals(top) ) 
{ 
middle = secondWord; 
} 
else 
{ 
middle = thirdWord; 
} 
System.out.println( top ); 
System.out.println( middle ); 
System.out.println( bottom ); 
}
}
Does anyone what I am missing or doing wrong? :( Please and thank you for any help!
© Stack Overflow or respective owner