How to check if a String is number, not a word?

W

www

Hi,

I need to check if a String is number, like "24.48936" or "-1.54774",
not "weight", or "length". How can I do that?

I know it need regular expression. But I am not good at it. Could you
help me?

Thank you.
 
O

Oliver Wong

www said:
Hi,

I need to check if a String is number, like "24.48936" or "-1.54774",
not "weight", or "length". How can I do that?

I know it need regular expression. But I am not good at it. Could you
help me?

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
 
S

Steve Wampler

www said:
Hi,

I need to check if a String is number, like "24.48936" or "-1.54774",
not "weight", or "length". How can I do that?

I know it need regular expression. But I am not good at it. Could you
help me?

You can do it without a regular expression if you want:

public static boolean method isNum(String s) {
try {
Double.parseDouble(s);
}
catch (NumberFormatException nfe) {
return false;
}
return true;
}

Of course, you're likely to be better off directly using the result
of parseDouble() as you're likely to need the result if it *is*
a number.
 
K

Knute Johnson

www said:
Hi,

I need to check if a String is number, like "24.48936" or "-1.54774",
not "weight", or "length". How can I do that?

I know it need regular expression. But I am not good at it. Could you
help me?

Thank you.

If they are always going to be floating point numbers as in your
example, use Integer.parseDouble() and if it throws a
NumberFormatException it isn't a number.
 
S

Steve Wampler

Knute said:
...
If they are always going to be floating point numbers as in your
example, use Integer.parseDouble() and if it throws a
NumberFormatException it isn't a number.

This should for integer values as well (though I think you mean
Double.parseDouble()).
 
K

Knute Johnson

Knute said:
If they are always going to be floating point numbers as in your
example, use Integer.parseDouble() and if it throws a
NumberFormatException it isn't a number.

Make that Double.parseDouble(). Integer.parseDouble() will get you no
where :).
 
J

Jasdeep

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top