...

View Full Version : How to put names in alphabetical order



sabi
02-19-2012, 06:47 AM
This is what I got but im getting an error in the if statements, so can someone help me fix this?


import java.util.Scanner;

public class Alpha
{
public static void main(String[] args)
{
String = name1;
String = name2;
String = name3;
Scanner keyboard = new Scanner(System.in);

System.out.println("Enter name one");
name1 = keyboard.nextLine();
System.out.println("Enter name two");
name2 = keyboard.nextLine();
System.out.println("Enter name three");
name3 = keyboard.nextLine();

if(name1.compareTo(name2) < 0) && (name1.compareTo(name3) < 0) && (name2.compareTo(name3) < 0)
{
System.out.println(name1 + name2 + name3);
}
else if(name2.compareTo(name1) < 0) && (name2.compareTo(name3) < 0) && (name1.compareTo(name3) < 0)
{
System.out.println(name2 + name1 + name3);
}
else if(name3.compareTo(name1) < 0) && (name3.compareTo(name2) < 0) && (name1.compareTo(name2) < 0)
{
system.out.println(name3 + name1 + name2);
}

}
}

alykins
02-19-2012, 06:56 AM
you can make this a lot easier on yourself if you add the strings to either an array or a list and then use the sort method
string array (http://www.leepoint.net/notes-java/data/arrays/70sorting.html)
collection (http://www.javamex.com/tutorials/collections/sorting_simple_cases.shtml)

sabi
02-19-2012, 07:54 AM
but why am i getting an error at the "Logical && " ....are you allowed to used the logical && with equals method in java?

Fou-Lu
02-19-2012, 05:10 PM
Because the if branch has been terminated: if(name1.compareTo(name2) < 0). Following && and anything after is a stynax error.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum