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.
LVL 5
apparitionAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
boolean valid = field.matches("\\d+");
0
 
InteractiveMindCommented:
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
}
0
 
InteractiveMindCommented:
*Sorry:

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

instead
0
 
CEHJCommented:
:-)
0
 
unshettyCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.