Link to home
Start Free TrialLog in
Avatar of apparition
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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
full points to CEHJ, but this would be a more efficient method to perform this, I imagine:

public boolean isNumeric( String field )
{
    return field.matched( "\\d+" );               // No idea how this works, but you're the man CEHJ
}
*Sorry:

     return field.matches( "\\d+" );

instead
:-)
Avatar of unshetty
unshetty

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.