-
Testing Strings
Hi,
Having a few problems with strings.
I have this code and it wont compile.
I am trying to write a program so that I can input two strings, and then tests the strings to see whether they are identical or, if not, which comes first in the alphabet.
Code:
public class Strings
{
public static void main (String[] args)
{
System.out.print("Please Enter First String: ");
string1 = EasyIn.getInt();
System.out.print("Please Enter Second String: ");
string2 = EasyIn.getInt();
if (string1.equals(string2))
System.out.println("The Strings are Identical");
else if (string1.compareTo(string2) >0 )
System.out.println("The String1 comes first in alphabetical Order");
else if (string1.compareTo(string2) < 0)
System.out.println("The String2 comes first in alphabetical order");
}
}
Can someone show me whats wrong with the code
Thanks
-
I have changed it and it now compiles:
Code:
public class Strings
{
public static void main (String[] args)
{
String string1, string2;
System.out.print("Please Enter First String: ");
string1 = EasyIn.getString();
System.out.print("Please Enter Second String: ");
string2 = EasyIn.getString();
if (string1.equals(string2))
System.out.println("The Strings are Identical");
else if (string1.compareTo(string2) >0 )
System.out.println("The First String comes first in alphabetical Order");
else if (string1.compareTo(string2) >0 )
System.out.println("The Second String comes first in alphabetical order");
}
}
The only problem is when i input two strings such as hello and goodbye, its saying goodbye comes first in the alphabet
-
Originally posted by T-Willis
I have changed it and it now compiles:
Code:
public class Strings
{
public static void main (String[] args)
{
String string1, string2;
System.out.print("Please Enter First String: ");
string1 = EasyIn.getString();
System.out.print("Please Enter Second String: ");
string2 = EasyIn.getString();
if (string1.equals(string2))
System.out.println("The Strings are Identical");
else if (string1.compareTo(string2) >0 )
System.out.println("The First String comes first in alphabetical Order");
else if (string1.compareTo(string2) >0 ) <--- uuups
System.out.println("The Second String comes first in alphabetical order");
}
}
Seems like the same test twice to me ...
The only problem is when i input two strings such as hello and goodbye, its saying goodbye comes first in the alphabet
eschew obfuscation
-
Fixed my problem.
I just had to change the greater and less signs.
-
Originally posted by T-Willis
The only problem is when i input two strings such as hello and goodbye, its saying goodbye comes first in the alphabet
...
goodbye does come before hello
-
Yes i realise that, sorry typing mistake
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks