what method do i need to use for the alphabetical order
Is it indexOf
if yes how can i use it

Problem 2: Alphabetize Three Words
Program Name: Alphabetize.java
Write a program to take three words as input then output those three words in alphabetical order. If all
three words input were the same, the program should instead print out the special message: "All three
of those words are the same!"
For this exercise, let alphabetizations be case sensitive. (e.g. "dog" isn't the same as "Dog")

``````import java.util.Scanner;

public class Three {

public static void main (String [] args) {

Scanner input = new Scanner (System.in);

System.out.println("Enter three words");

String s1, s2, s3;

s1 = input.next();
s2 = input.next();
s3 = input.next();
int compare = s1.compareTo(s2 + s3);

if (compare < 0)

System.out.print("All three of those words are the same ! " + s1 + " " + s2 + " " + s3);

else if (compare > 0)

System.out.print("In alphabetical order those are:" + s1 + " " + s2 + " " + s3 + s1);

}
}``````

## All 8 Replies

what method do i need to use for the alphabetical order

You are using the method: compareTo that I would suggest.
Can you explain what your problem is with the program?

what method i need to use to do the alphabetical order ?

That is the sample output

Problem 2 Sample Output
C:> java Alphabetize
Please enter three words: ant cat dog
In alphabetical order those are: ant cat dog
C:> java Alphabetize
Please enter three words: ant dog cat
In alphabetical order those are: ant cat dog
C:> java Alphabetize
Please enter three words: cat ant dog
In alphabetical order those are: ant cat dog
C:> java Alphabetize
Please enter three words: cat dog ant
In alphabetical order those are: ant cat dog
C:> java Alphabetize
Please enter three words: dog ant cat
In alphabetical order those are: ant cat dogC:> java Alphabetize
Please enter three words: dog cat ant
In alphabetical order those are: ant cat dog
C:> java Alphabetize
Please enter three words: ant ant cat
In alphabetical order those are: ant ant cat
C:> java Alphabetize
Please enter three words: ant cat ant
In alphabetical order those are: ant ant cat
C:> java Alphabetize
Please enter three words: cat ant ant
In alphabetical order those are: ant ant cat
C:> java Alphabetize
Please enter three words: ant ant ant
All three of those words are the same!

Can you explain what the problem with the output you posted is?
Add some comments that show the error and show what the output should be.

You can try something like this :

``````import java.util.*;

public class Order {

static List<String> myList = new ArrayList<String>();

public static void main(String[] args) {

takeWords();
}

public static void takeWords() {
Order myOrder = new Order();
Scanner input = new Scanner(System.in);

System.out.println("Please enter how many words you want to take :");
int numWords = input.nextInt();
for(int i=0; i<numWords; i++) {
System.out.print((i+1) + " - ");
}
System.out.println("Above words in order :");
myOrder.order();

}
public void order() {

Collections.sort(this.myList);
Iterator itr = this.myList.iterator();
String str = null;
while(itr.hasNext()) {
str = (String) itr.next();
System.out.printf("%s ",str);
}

}

}``````
commented: spoonfeeding code -3

@shean1488
Waste of time. The OP doesn't know about Collections and Iterators.
Instead of showing off by posting code,
Why don't you help the OP work through the logic he needs instead of feeding him code that he won't understand?

when i put 3 different words
the output is they are the same words !!
why !

Waste of time. The OP doesn't know about Collections and Iterators.
Instead of showing off by posting code,
Why don't you help the OP work through the logic he needs instead of feeding him code that he won't understand?

I wasn't showing off, I made similar methods some time ago and just modifyed it a bit and post it, just in case it would help

when i put 3 different words
the output is they are the same words !!

You need to think what the steps are for your program before writing the code.
If you have three variables with values that you want to sort, what are the comparisons you need to make to find the lowest one, the next higher and the highest one?
Do you know how to use arrays and loops?

If not you will need to use some if statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.