Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is CharUtils.isAsciiNumeric in Java?

abhilash

isAsciiNumeric is a static method of the CharUtils class that is used to check whether the input character is a single-digit from zero to nine. The method returns true if the ASCII value of the character is in the range of 48 to 57, inclusive.

How to import CharUtils

CharUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.

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

For other versions of the commons-lang package, refer to the Maven Repository.

We can import the CharUtils class as follows.

import org.apache.commons.lang3.CharUtils;

Syntax


public static boolean isAsciiNumeric(final char ch)

Parameters

  • final char ch: the character to check

Return value

The function returns true if the character is in the range of 49 to 57inclusive; otherwise, it returns false.

Code

import org.apache.commons.lang3.CharUtils;

public class Main {

    public static void main(String[] args) {
        char c1 = 'F';
        System.out.printf("The output of CharUtils.isAsciiNumeric() for the character '%s' is %s", c1, CharUtils.isAsciiNumeric(c1));
        System.out.println();

        c1 = 'È';
        System.out.printf("The output of CharUtils.isAsciiNumeric() for the character '%s' is %s", c1, CharUtils.isAsciiNumeric(c1));
        System.out.println();

        c1 = '9';
        System.out.printf("The output of CharUtils.isAsciiNumeric() for the character '%s' is %s", c1, CharUtils.isAsciiNumeric(c1));
        System.out.println();

        c1 = '5';
        System.out.printf("The output of CharUtils.isAsciiNumeric() for the character '%s' is %s", c1, CharUtils.isAsciiNumeric(c1));
        System.out.println();
    }
}

Explanation

  • character - F

    The method returns false, as the character is not a digit in the range of zero to nine.

  • character - È

    The method returns false, as the character is not a digit in the range of zero to nine.

  • character - 9

    The method returns true, as the character is a digit in the range of zero to nine.

  • character - 5

    The method returns true, as the character is a digit in the range of zero to nine.

Expected output


The output of CharUtils.isAsciiNumeric() for the character 'F' is false
The output of CharUtils.isAsciiNumeric() for the character 'È' is false
The output of CharUtils.isAsciiNumeric() for the character '9' is true
The output of CharUtils.isAsciiNumeric() for the character '5' is true```

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring