Java Check Character is ASCII Numeric using Apache Commons Lang

Tags: Apache Commons Apache Commons Lang CharUtils char Character ASCII

In this Java tutorial we learn how to check whether a character is an ASCII numeric character using the CharUtils class of Apache Commons Lang library.

How to add Apache Commons Lang 3 library to your Java project

To use the Apache Commons Lang 3 library in the Gradle build project, add the following dependency into the build.gradle file.

implementation 'org.apache.commons:commons-lang3:3.12.0'

To use the Apache Commons Lang 3 library in the Maven build project, add the following dependency into the pom.xml file.

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

To have more information about the Apache Commons Lang 3 library you can visit the library home page at commons.apache.org/proper/commons-lang/

How to check character is an ASCII numeric character in Java

The Apache Commons Lang library provides the method CharUtils.isAsciiNumeric() to check whether a given character is an ASCII character or not. You can learn how to use the CharUtils.isAsciiNumeric() method via the example code below.

CheckIsAsciiNumericChar.java

import org.apache.commons.lang3.CharUtils;

public class CheckIsAsciiNumericChar {
    public static void main(String... args) {
        boolean result1 = CharUtils.isAsciiNumeric('a');
        boolean result2 = CharUtils.isAsciiNumeric('A');
        boolean result3 = CharUtils.isAsciiNumeric('5');
        boolean result4 = CharUtils.isAsciiNumeric('\n');
        boolean result5 = CharUtils.isAsciiNumeric('-');

        System.out.println("Character 'a' is ASCII numeric: " + result1);
        System.out.println("Character 'A' is ASCII numeric: " + result2);
        System.out.println("Character '5' is ASCII numeric: " + result3);
        System.out.println("Character '\\n' is ASCII numeric: " + result4);
        System.out.println("Character '-' is ASCII numeric: " + result5);
    }
}
The output is:
Character 'a' is ASCII numeric: false
Character 'A' is ASCII numeric: false
Character '5' is ASCII numeric: true
Character '\n' is ASCII numeric: false
Character '-' is ASCII numeric: false

Happy Coding 😊