Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing Strings

 
Timothy Willis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Having a few problems with strings. I am trying to write a program so that I can input two strings, and then test the strings to see whether they are identical or, if not, which comes first in the alphabet.

I have this code and it wont compile.



Any help would be appreciated.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error message are you getting?
 
Timothy Willis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error message:

Cannot resolve symbol - variable string 1
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you don't declare string1 anywhere, do you? A declaration include the variables type and name and optionally an initializer. You first use "string1" without having every declared it to be a String.

When you do, I suspect you'll have a problem assigning what "getInt()" returns to a String variable -- maybe there's a getString() method you should use instead?
 
Timothy Willis
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So like this:

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

}
}
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "EasyIn.getString()"?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic