Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Textfield comparison

 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How I can compare the value of textfied to character(non-numbers)?
Thanks
angela
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic