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

Discussion in 'Java' started by www, Jul 24, 2007.

  1. www

    www Guest

    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.
    www, Jul 24, 2007
    #1
    1. Advertising

  2. www

    Oliver Wong Guest

    "www" <> wrote in message
    news:f85m93$h10$...
    > 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
    Oliver Wong, Jul 24, 2007
    #2
    1. Advertising

  3. 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 --
    The gods that smiled on your birth are now laughing out loud.
    Steve Wampler, Jul 24, 2007
    #3
  4. 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/
    Knute Johnson, Jul 24, 2007
    #4
  5. 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 --
    The gods that smiled on your birth are now laughing out loud.
    Steve Wampler, Jul 24, 2007
    #5
  6. 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/
    Knute Johnson, Jul 24, 2007
    #6
  7. www

    Jasdeep Guest

    On Jul 25, 1:49 am, "Oliver Wong" <> wrote:
    > "www" <> wrote in message
    >
    > news:f85m93$h10$...
    >
    > > 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(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
    Jasdeep, Jul 25, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    933
    X-Centric
    Jun 30, 2005
  2. kathy
    Replies:
    3
    Views:
    737
  3. Jochen Brenzlinger
    Replies:
    5
    Views:
    687
    Daniel Pitts
    Nov 21, 2011
  4. Brajmohan S.

    Comparing a string word by word

    Brajmohan S., Jan 17, 2011, in forum: Ruby
    Replies:
    2
    Views:
    116
    Robert Klemme
    Jan 19, 2011
  5. babu17
    Replies:
    1
    Views:
    136
Loading...

Share This Page