Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to check if a String is number, not a word?

Reply
Thread Tools

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

 
 
www
Guest
Posts: n/a
 
      07-24-2007
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.
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-24-2007
"www" <(E-Mail Removed)> wrote in message
news:f85m93$h10$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Steve Wampler
Guest
Posts: n/a
 
      07-24-2007
www wrote:
> 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.

--
Steve Wampler -- http://www.velocityreviews.com/forums/(E-Mail Removed)
The gods that smiled on your birth are now laughing out loud.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      07-24-2007
www wrote:
> 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.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Steve Wampler
Guest
Posts: n/a
 
      07-24-2007
Knute Johnson wrote:
> www wrote:
>> I need to check if a String is number, like "24.48936" or "-1.54774",
>> not "weight", or "length". How can I do that?

>...
> 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()).


--
Steve Wampler -- (E-Mail Removed)
The gods that smiled on your birth are now laughing out loud.
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      07-24-2007
Knute Johnson wrote:
> www wrote:
>> 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.
>


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

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Jasdeep
Guest
Posts: n/a
 
      07-25-2007
On Jul 25, 1:49 am, "Oliver Wong" <(E-Mail Removed)> wrote:
> "www" <(E-Mail Removed)> wrote in message
>
> news:f85m93$h10$(E-Mail Removed)...
>
> > 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


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(decimalPatter n);
}else{
matchResult=stringYouEntered.matches(integerPatter n);
}

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
To check or not to check for NULL? Mike Java 12 09-26-2008 10:46 PM
How to check if all *.jars in my CLASSPATH really exists? Automatic check (not manual) possible? Jason Stacy Java 2 04-27-2008 08:14 AM
Python style: to check or not to check args and data members Joel Hedlund Python 18 09-03-2006 05:52 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM



Advertisments