Is it a number (java)

Discussion in 'OT Technology' started by sk0rcher, Apr 1, 2008.

  1. sk0rcher

    sk0rcher The universe is watching

    Joined:
    Jun 18, 2004
    Messages:
    1,704
    Likes Received:
    0
    Location:
    Roseville
    Hi I am building a post fix expression evaluator in java and I am having some trouble...what should I do to identify a number in a string is a number? Decimal numbers included. Basically throwing out anything that isn't a number or +, -, /, *. Help would be great. Thanks.
     
  2. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    String strValue = "242.94";
    double dblValue;

    try {
    dblValue = Double.parseDouble(strValue);
    // if the code gets to here, it was recognizeable as a double
    }
    catch(NumberFormatException e) {
    // if the code gets to here, it was NOT recognizeable as a double
    }
     
  3. sk0rcher

    sk0rcher The universe is watching

    Joined:
    Jun 18, 2004
    Messages:
    1,704
    Likes Received:
    0
    Location:
    Roseville
    Thanks...is there any other way of doing this other than using an exception?
     
    Last edited: Apr 2, 2008
  4. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    maybe. I would code it with try/catch.
     
  5. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
  6. sk0rcher

    sk0rcher The universe is watching

    Joined:
    Jun 18, 2004
    Messages:
    1,704
    Likes Received:
    0
    Location:
    Roseville
    What do you have to import for that to work? Sorry still kinda new at this...or do u have to dl that package and put it in a folder somewhere...
     
  7. Nocera

    Nocera ...

    Joined:
    Aug 9, 2000
    Messages:
    1,307
    Likes Received:
    0
    Location:
    Long Island, NY
    You can download it from here:
    http://commons.apache.org/downloads/download_math.cgi

    Unzip it and put the jar file on your classpath.

    If this is a small, one class project (ie a homework assignment), then it's probably overkill to add it to your project. If that's the case, just use the Double.parseDouble() method like P07r0457 mentioned.
     
  8. sk0rcher

    sk0rcher The universe is watching

    Joined:
    Jun 18, 2004
    Messages:
    1,704
    Likes Received:
    0
    Location:
    Roseville
    Ill use yours, thanks. The teacher wants it to work really well so I'll just use it.
     
  9. Jimmy10to2

    Jimmy10to2 New Member

    Joined:
    Nov 20, 2006
    Messages:
    126
    Likes Received:
    0
    I don't see how a catch NumberFormatException is going to help parse-out non-digits from a String. Only thing that is going to do is return an exception if there are non-digits in the String (excluding for doubles). Re-reading the OPs original question, it's a little confusing what he is actually trying to do. It's late. Perhaps the proper solution was already given, idk since the original question was never very clear.

    String str = ???;
    String result = "";

    for (int i = 0; i < str.length(); i++) {
    if (Character.isDigit(str.charAt(i)) == true || str.charAt(i) == '.')
    result += str.substring(i, i+1);
    }

    System.out.println(result);
     
    Last edited: Apr 3, 2008

Share This Page