Low Orbit Flux Logo 2 D

Java How to Check If a String is a Number

When you need to handle text data you can eventually expect to come across situations where you need to check if a string is a number. We are going to show you how to do this in Java.

Quick answer to check if a string is a number in Java:

boolean a = Pattern.compile("-?\\d+(\\.\\d+)?").matcher(string1).matches();

See below for the full example and for a few other methods.

Best Method - Regex

This first method is my favorite and is therefore the best method. There are other ways of doing the same thing but I think this is the best solution. You will need to remember to import the regex libraries. It might not seem straightforward if you don’t understand regexes but it has the advantage of not requiring you to deal with exception handling or third party libraries.

import java.util.regex.Pattern;

class Test1 {
    public static void main(String[] args) {
             String string1 = "test";
             String string2 = "123";
             String string3 = "123.45";

             boolean a = Pattern.compile("-?\\d+(\\.\\d+)?").matcher(string1).matches();
             boolean b = Pattern.compile("-?\\d+(\\.\\d+)?").matcher(string2).matches();
             boolean c = Pattern.compile("-?\\d+(\\.\\d+)?").matcher(string3).matches();

             System.out.println(a);    // false
             System.out.println(b);    // true
             System.out.println(c);    // true
    }
}

Apache Commons

One fast way to check if a string is a number is to use NumberUtils from Apache Commons. I still prefer regexes but this works.

Apache Commons 3.5 and up:

NumberUtils.isCreatable("123.45")
StringUtils.isNumeric("123.45")

Apache Commons 3.4 and down:

NumberUtils.isNumber("123.45")
StringUtils.isNumeric("123.45")

Java - Check if a String is a Number With Double.parseDouble()

The following example shows how to create your own function that will test if a string is a number. This uses the static parseDouble() method from the Double class. It should work fine for identifying most numbers including int, float, and double.

import java.util.regex.Pattern;


class Test1 {


    public static boolean testIsNumber(String string1) {
        if (string1 == null) {
            return false;
        }
        try {
            double d1 = Double.parseDouble(string1);
        } catch (NumberFormatException e1) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
             String string1 = "test";
             String string2 = "123";
             String string3 = "123.45";

             boolean a = testIsNumber(string1);
             boolean b = testIsNumber(string2);
             boolean c = testIsNumber(string3);

             System.out.println(a);    // false
             System.out.println(b);    // true
             System.out.println(c);    // true
    }
}

You might also want to work in other methods such as these for a more complete solution: