Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input String Comparison

Status
Not open for further replies.

jodders

Programmer
Nov 12, 2003
13
0
0
GB
how do you compare a user input to a a given string?

for example,Iam creating a game for kids,and what happens is, a picture of a triangle appears,and the kid has to type in triangle to win.

 
So where is the problem?

Do you have a JTextbox for the input?
Then you can use it's method:

JTextbox jt = new JTextbox (20); // 20 characters length
...
String input = jt.getText ();

if (input.equals ("triangle"))
// ...;

// If you don't care about upper- or lowercase:
String lower = input.toLowerCase ();
if (lower.equals ("triangle")) // ...

 
Is there a performance preference for doing a two-step toLowerCase and then equals? Why not just .equalsIgnoreCase()? (I suppose I could run a test... :) )
 
Why a two-step?
If you know the answer is written 'triangle', you only need to lower the userinput.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top