Checking whether a string is a valid number

  • I'm developing a Java Helper Library which has useful methods developers don't want to type out over and over. I'm trying to think of the best way of checking whether a string is a valid number.

    I've always just tried something like Integer.parseInt(iPromiseImANumber); and if it throws an exception then it's not a valid number (integer in this case). I'm thinking that trying to catch an exception isn't good practice. Is there a better way to do this? (I'm thinking a regular expression could do it, but it doesn't seem to be as reliable as the parse option).

    Here's my method as it stands. What could be improved?

    * Does a try catch (Exception) on parsing the given string to the given type
    * @param c the number type. Valid types are (wrappers included): double, int, float, long.
    * @param numString number to check
    * @return false if there is an exception, true otherwise
    public static boolean isValidNumber(Class c, String numString) {
    try {
    if (c == double.class || c == Double.class) {
    } else if (c == int.class || c == Integer.class) {
    } else if (c == float.class || c == Float.class) {
    } else if (c == long.class || c == Long.class) {
    } catch (Exception ex) {
    return false;
    return true;

    What exactly do you consider a "valid number"? IMHO in most use cases a "valid number" doesn't necessarily mean it has to be a valid Java Integer/Long/Float/Double.

    What else could it be?

    I'm guessing that @RoToRa is referring to range checking. It won't matter if the value can be legally parsed to a given type if it is outside the range of reasonable/permitted values.

    @Donald.McLean Basically, yes. It's unlikely that a real-life application has the same requirements to a "number" (range, decimal places, format) that the Java `parse*` methods allow.

    Maybe I'm still not quite getting you, but the purpose of this method is to check whether a string can be parsed as an actual number, not whether it fulfills application-level requirements. Those can be handled by the application once the number's been parsed. But maybe I'm misunderstanding you...

    @kentcdodds But what is the point of having a program know if a string can be parsed as a number, especially if you want to parse it as a number anyway? You call `parse*` twice, first check for an exception, but throw away the result, and then again to get the number? Can you give a real life use case where you "just" need to know if a string is any kind of number.

    @RoToRa, that's a good point. But how do you recommend as a return value in the case the given string is invalid? I suppose I could have the return be a wrapper and if it's invalid have the wrapper be null. But I don't know if that'd be as helpful. Do you have any other recommendations?

  • palacsint

    palacsint Correct answer

    9 years ago

    See my benchmarking answer. This was definitely the fastest. Thanks for the tip!

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM