Check if a String is a valid numeric value in Java using Apache Commons Lang

Tags: Apache Commons Apache Commons Lang NumberUtils StringUtils String Number

Introduction

In this article we show you how to use different utility classes in Apache Commons Lang library to check if a String is a valid number in Java. We provide different working code examples how to use methods such as NumberUtils.isCreatable(), NumberUtils.isDigits() or StringUtils.isNumeric().

Setup Apache Commons Lang in Java project

If you are using Gradle build then add the following dependency configuration into build.gradle file.

compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.11'

Or add the following dependency XML tag to pom.xml file if you are using Maven build.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>

Or download commons-lang3-3.11.jar file from Apache Commons Lang download page at commons.apache.org

How to use org.apache.commons.lang3.math.NumberUtils.isCreatable()

Following Java code example shows how to use NumberUtils.isCreatable() method to check whether an input String is a valid Java number.

import org.apache.commons.lang3.math.NumberUtils;

public class NumberUtilsIsCreatableExample {
    public static void main(String[] args) {
        boolean isNumeric1 = NumberUtils.isCreatable(null);
        System.out.println("null: " + isNumeric1);

        boolean isNumeric2 = NumberUtils.isCreatable("");
        System.out.println("empty String : " + isNumeric2);

        boolean isNumeric3 = NumberUtils.isCreatable(" ");
        System.out.println("space : " + isNumeric3);

        boolean isNumeric4 = NumberUtils.isCreatable("abc");
        System.out.println("abc : " + isNumeric4);

        boolean isNumeric5 = NumberUtils.isCreatable("1-2");
        System.out.println("1-2 : " + isNumeric5);

        boolean isNumeric6 = NumberUtils.isCreatable("1 2");
        System.out.println("1 2 : " + isNumeric6);

        boolean isNumeric7 = NumberUtils.isCreatable("-1");
        System.out.println("-1 : " + isNumeric7);

        boolean isNumeric8 = NumberUtils.isCreatable("+1");
        System.out.println("+1 : " + isNumeric8);

        boolean isNumeric9 = NumberUtils.isCreatable("1.2");
        System.out.println("1.2 : " + isNumeric9);

        boolean isNumeric10 = NumberUtils.isCreatable("12");
        System.out.println("12 : " + isNumeric10);
    }
}
Output:

null: false
empty String : false
space : false
abc : false
1-2 : false
1 2 : false
-1 : true
+1 : true
1.2 : true
12 : true

How to use org.apache.commons.lang3.math.NumberUtils.isDigits()

The Java application below shows how to use NumberUtils.isDigits() method to check whether the String contains only digit characters.

import org.apache.commons.lang3.math.NumberUtils;

public class NumberUtilsIsDigitsExample {
    public static void main(String[] args) {
        boolean isDigits1 = NumberUtils.isDigits(null);
        System.out.println("null: " + isDigits1);

        boolean isDigits2 = NumberUtils.isDigits("");
        System.out.println("empty String : " + isDigits2);

        boolean isDigits3 = NumberUtils.isDigits(" ");
        System.out.println("space : " + isDigits3);

        boolean isDigits4 = NumberUtils.isDigits("abc");
        System.out.println("abc : " + isDigits4);

        boolean isDigits5 = NumberUtils.isDigits("1-2");
        System.out.println("1-2 : " + isDigits5);

        boolean isDigits6 = NumberUtils.isDigits("1 2");
        System.out.println("1 2 : " + isDigits6);

        boolean isDigits7 = NumberUtils.isDigits("-1");
        System.out.println("-1 : " + isDigits7);

        boolean isDigits8 = NumberUtils.isDigits("+1");
        System.out.println("+1 : " + isDigits8);

        boolean isDigits9 = NumberUtils.isDigits("1.2");
        System.out.println("1.2 : " + isDigits9);

        boolean isDigits10 = NumberUtils.isDigits("12");
        System.out.println("12 : " + isDigits10);
    }
}
Output:

null: false
empty String : false
space : false
abc : false
1-2 : false
1 2 : false
-1 : false
+1 : false
1.2 : false
12 : true

How to use org.apache.commons.lang3.StringUtils.isNumeric()

The StringUtils class provides isNumeric() to check if an input String contains only Unicode digits characters.

import org.apache.commons.lang3.StringUtils;

public class StringUtilsIsNumericExample {
    public static void main(String[] args) {
        boolean isNumeric1 = StringUtils.isNumeric(null);
        System.out.println("null: " + isNumeric1);

        boolean isNumeric2 = StringUtils.isNumeric("");
        System.out.println("empty String : " + isNumeric2);

        boolean isNumeric3 = StringUtils.isNumeric(" ");
        System.out.println("space : " + isNumeric3);

        boolean isNumeric4 = StringUtils.isNumeric("abc");
        System.out.println("abc : " + isNumeric4);

        boolean isNumeric5 = StringUtils.isNumeric("1-2");
        System.out.println("1-2 : " + isNumeric5);

        boolean isNumeric6 = StringUtils.isNumeric("1 2");
        System.out.println("1 2 : " + isNumeric6);

        boolean isNumeric7 = StringUtils.isNumeric("-1");
        System.out.println("-1 : " + isNumeric7);

        boolean isNumeric8 = StringUtils.isNumeric("+1");
        System.out.println("+1 : " + isNumeric8);

        boolean isNumeric9 = StringUtils.isNumeric("1.2");
        System.out.println("1.2 : " + isNumeric9);

        boolean isNumeric10 = StringUtils.isNumeric("12");
        System.out.println("12 : " + isNumeric10);

        boolean isNumeric11 = StringUtils.isNumeric("\u0967\u0968\u0969");
        System.out.println("\\u0967\\u0968\\u0969 : " + isNumeric11);
    }
}
Output:

null: false
empty String : false
space : false
abc : false
1-2 : false
1 2 : false
-1 : false
+1 : false
1.2 : false
12 : true
\u0967\u0968\u0969 : true

Happy Coding 😊