2

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;

                      }
              }
        }
  }
Young Emil
  • 2,220
  • 2
  • 26
  • 37
Derrin
  • 29
  • 5

1 Answers1

0
final String[] parts = words.split(" ");

if(parts[0].equals(parts[1]) && parts[1].equals(parts[2]) {
     System.out.println("These words are the exact same");
}

.equals compares the contents of Strings. If string 1 equals string 2 and string 2 equals string 3, string 1 equals string 3 by the transitive property.