The following code alphabetizes three words in a user input string.
Example:
cat ant dog
Output:
ant cat dog
What I'm trying to do, is make it so that if the three words are completely identical, it'll print out "these words are the exact same" or something on the lines of that.
I'm not quite sure how to go about doing that. Any help or advice would be appreciated, thank you!
final Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter three words : ");
final String words = keyboard.nextLine();
final String[] parts = words.split(" ");
System.out.print("In alphabetical order those are: ");
alphabetizing (parts);
for ( int k = 0; k < 3; k++ )
System.out.print( parts [k] + " " );
public static void alphabetizing( String x [] )
{
int word;
boolean check = true;
String temp;
while ( check )
{
check = false;
for ( word = 0; word < x.length - 1; word++ )
{
if ( x [ word ].compareToIgnoreCase( x [ word+1 ] ) > 0 )
{
temp = x [ word ];
x [ word ] = x [ word+1];
x [ word+1] = temp;
check = true;
}
}
}
}