How can i make the words in alphabetical order if there are not the same ???

import java.util.Scanner;

public class Alphabetize{

	public static void main (String [] args) {
		
		Scanner input = new Scanner (System.in);
		
		System.out.println("Enter three words");
		
		String Word1, Word2, Word3;
		
		Word1 = input.next();
		Word2 = input.next();
		Word3 = input.next();
		
		boolean A = Word1.matches(Word2);
		boolean B = Word2.matches(Word3);
		boolean C = Word3.matches(Word1);
		boolean D = Word3.matches(Word2);
	
		if (A==true)
		{
			if (B==true)
			if (C==true)	
			if (D==true)
			System.out.println("All three of those words are the same :" + Word1 +" "+ Word2+" "+ Word3);
		}
		else 
		{
			System.out.println("In alphabetical order those are :" + Word1 +" "+ Word2+" "+ Word3 );
		}
	}
}
Member Avatar for ztini
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Alphabetize {

	public static void main (String [] args) {
		
		Scanner input = new Scanner (System.in);
		
		System.out.println("Enter three words");
		
//		String Word1, Word2, Word3;
//		
//		Word1 = input.next();
//		Word2 = input.next();
//		Word3 = input.next();
		
		// variables are camelCase with first letter lower
		// Classes are CamelCase with first letter capital
		String word1 = input.next();
		String word2 = input.next();
		String word3 = input.next();
		
		// Create a list
		List<String> wordList = new ArrayList<String>();
		
		// Add your words to the list
		wordList.add(word1);
		wordList.add(word2);
		wordList.add(word3);
		
		// Let Collections do the sort..
		Collections.sort(wordList);
		
		// Loop over the sorted list
		System.out.print("In alphabetical order those are: ");
		for (String word : wordList)
			System.out.print(MessageFormat.format("{0} ", word));
		
//		boolean A = Word1.matches(Word2);
//		boolean B = Word2.matches(Word3);
//		boolean C = Word3.matches(Word1);
//		boolean D = Word3.matches(Word2);
	
//		if (A==true)
//		{
//			if (B==true)
//			if (C==true)	
//			if (D==true)
//			System.out.println("All three of those words are the same :" + Word1 +" "+ Word2+" "+ Word3);
//		}
//		else 
//		{
//			System.out.println("In alphabetical order those are :" + Word1 +" "+ Word2+" "+ Word3 );
//		}
	}
}

Output:

Enter three words
foo
bar
rawr
In alphabetical order those are: bar foo rawr

Thank you very much

But when i put three same words it says In alphabetical order those are: foo foo foo

and is there a way to do it by if statement ?

Since the code have been handed down to you we might as well go with it :-/

Anyway did you notice those those commented lines with tells when all three words are the same
Try to modify the code so that it will first check if all the words are equal then if not sort them

Ok, Thanks a lot guys

Member Avatar for ztini

Can you post your code which is outputting the incorrect values?

If you want to use an if statement, then you need to implement a sorting algorithm. There are many flavors to choose from: http://en.wikipedia.org/wiki/Sorting_algorithm

But, why reinvent the wheel when the Collections class has already done all the work for you?

Furthermore, Collections.sort(List) is a modified merge sort algorithm, which is Omega(n log n) time. Meaning, its very efficient.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List)

Thank you very much

But when i put three same words it says In alphabetical order those are: foo foo foo

well ... duh.
about your if statement question: yes, there is.
String implements the comparable interface, so you could use the compareTo method.

run a check on the value returned to check whether or not to switch places

appreciate your helping me guys

but is this the right way of using method compare to ?
i knew i have an errors but i couldn't fix them

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);
	}

	
}
}

I think you'll need to
int firstCompare = s1.compareTo(s2);
and
int secondCompare = s1.compareTo(s3);

and this for all your comparisons

Thanks

i tried it do that but what should i add in the if statement

i mean how can i put the secondCompare into the if statement

by using logical operators:

if both have to be true:

if ( expression1 && expression2 )

if (at least) one of them has to be true:

if ( expression1 || expression2 )

or you could add it in a nested if:

if ( expression1 ){
  if ( expression2 ){
  }
}
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.