apparition
asked on
Validation method
I need help writing a method that determines if all the characters in a passed string are numbers.
method skeleton :
========================== ========== =
public boolean isNumeric (String field){
boolean isValid;
//determine if string all numbers
//if string all numbers isValid = true;
//else isValid = false;
return isValid;
}
Please help.
method skeleton :
==========================
public boolean isNumeric (String field){
boolean isValid;
//determine if string all numbers
//if string all numbers isValid = true;
//else isValid = false;
return isValid;
}
Please help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
*Sorry:
return field.matches( "\\d+" );
instead
return field.matches( "\\d+" );
instead
:-)
It is all about patterns, regular expression etc.
d indicates a digit ( 0 to 9)
+ indicates one or more.
So, matches method returns true if the field has one or more digits and nothing else. Otherwise it will return false.
d indicates a digit ( 0 to 9)
+ indicates one or more.
So, matches method returns true if the field has one or more digits and nothing else. Otherwise it will return false.
public boolean isNumeric( String field )
{
return field.matched( "\\d+" ); // No idea how this works, but you're the man CEHJ
}