Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
www - 24 Jul 2007 21:09 GMT
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.
Oliver Wong - 24 Jul 2007 21:49 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> 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
Jasdeep - 25 Jul 2007 07:47 GMT
> > Hi,
>
[quoted text clipped - 14 lines]
>
>     - 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
Steve Wampler - 24 Jul 2007 22:08 GMT
> Hi,
>
[quoted text clipped - 3 lines]
> 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.

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Knute Johnson - 24 Jul 2007 22:25 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Steve Wampler - 24 Jul 2007 22:47 GMT
>> 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()).

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Knute Johnson - 24 Jul 2007 22:57 GMT
>> Hi,
>>
[quoted text clipped - 9 lines]
> 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 :-).

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.