This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Textfield comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Textfield comparison" Watch "Textfield comparison" New topic
Author

Textfield comparison

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
How I can compare the value of textfied to character(non-numbers)?
Thanks
angela
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
I believe you can accomplish what you want by doing the following
String somValue = "some string";
String fieldValue = yourTextField.getText();
// check for (in)equality
if ( fieldValue.euqals( someValue ) )
{}
You may also want to look at java.lang.String.trim() method which can be used to remove whitespace from beginning and end of String.
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
But it could be any string from textfield. I have to discriminate whether it's non numbers or number
Thanks
Angela
Originally posted by timothy zimmerman:
I believe you can accomplish what you want by doing the following
String somValue = "some string";
String fieldValue = yourTextField.getText();
// check for (in)equality
if ( fieldValue.euqals( someValue ) )
{}
You may also want to look at java.lang.String.trim() method which can be used to remove whitespace from beginning and end of String.

Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Angela,
Maybe you need to be a little clearer in exactly what it is you are wanting. Are you wanting to know if what is entered in the TextField is numbers or not?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You can just try stuffing it into a double, and put that statement in a try/catch block so that if it is NOT numeric the catch block gets executed.
try{
double d = Double.parseDouble(myString);
}
catch (NumberFormatException e){
System.out.println("Only enter numerics please"); //or whatever
}


"JavaRanch, where the deer and the Certified play" - David O'Meara
Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Thanks to all, I solved the problem by using Try and catch.
Angela
Originally posted by Cindy Glass:
You can just try stuffing it into a double, and put that statement in a try/catch block so that if it is NOT numeric the catch block gets executed.
try{
double d = Double.parseDouble(myString);
}
catch (NumberFormatException e){
System.out.println("Only enter numerics please"); //or whatever
}

 
 
subject: Textfield comparison