Step 1: Define the exact rules for determining whether or not a given
string is a "number", depending on what your definition of number is. E.g.
is "five" a number? What about "XXIV"? Is any string containing solely
digits a number (e.g. "00000")? Does the presence of whitespace mean it's
not a number (e.g. " 12 ")? Etc.
Step 2: Post your rules, and we can help you with the regular
expression part.
- Oliver
Try This Code:
/**
*patteren to match whether entered number is 3 or less digits
followed by dot followed by 3 or *less digits
*/
String decimalPattern = "(\\d{1,3})?(.)?(\\d{1,3})?";
/**
*patteren to match whether entered number 3 digit integer
*/
String integerPattern="(\\d{1,3})";
String stringYouEntered="545.12";
boolean matchResult=false;
if(stringYouEntered.contains(".")){
matchResult=stringYouEntered.matches(decimalPattern);
}else{
matchResult=stringYouEntered.matches(integerPattern);
}
if(matchResult){
System.out.prinln("I Got It Right, Number you Entred matches my
patteren");
}
... It works for me.. Hope it will help you...
-Jasdeep