Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-19-2012, 05:47 AM   PM User | #1
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
How to put names in alphabetical order

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

}
}
sabi is offline   Reply With Quote
Old 02-19-2012, 05:56 AM   PM User | #2
alykins
Senior Coder

 
alykins's Avatar
 
Join Date: Apr 2011
Posts: 1,608
Thanks: 37
Thanked 183 Times in 182 Posts
alykins will become famous soon enough
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
collection
__________________

I code C hash-tag .Net
Reference: W3C W3CWiki .Net Lib
Validate: html CSS
Debug: Chrome FireFox IE
alykins is offline   Reply With Quote
Old 02-19-2012, 06:54 AM   PM User | #3
sabi
New Coder

 
Join Date: Oct 2011
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
sabi is an unknown quantity at this point
but why am i getting an error at the "Logical && " ....are you allowed to used the logical && with equals method in java?
sabi is offline   Reply With Quote
Old 02-19-2012, 04:10 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Because the if branch has been terminated: if(name1.compareTo(name2) < 0). Following && and anything after is a stynax error.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:16 AM.


Advertisement
Log in to turn off these ads.