What is NumberUtils.isDigits() in Java?

Overview

isDigits() is a staticthe methods in Java that can be called without creating an object of the class. method of the NumberUtils that is used to check if the input string contains only digits. The method returns false if the input is null or an empty string.

How to import NumberUtils

The definition of NumberUtils can be found in the Apache Commons Lang package, which we can add 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.

You can import the NumberUtils class as follows:


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

Syntax


public static boolean isDigits(final String str)

Parameters

  • final String str: This is the string to check.

Return value

This method returns true if the string contains only digits. Otherwise, it returns false.

Code

import org.apache.commons.lang3.math.NumberUtils;
public class Main{
public static void main(String[] args){
// Example 1
String stringToConvert = "23356";
System.out.printf("The output of the method NumberUtils.isDigits(%s) is %s", stringToConvert, NumberUtils.isDigits(stringToConvert));
System.out.println();
// Example 2
stringToConvert = "233sdf";
System.out.printf("The output of the method NumberUtils.isDigits(%s) is %s", stringToConvert, NumberUtils.isDigits(stringToConvert));
System.out.println();
}
}

Example 1

  • string to convert = 23356

The method returns true, as the input string contains only digits.

Example 2

  • string to convert = 233sdf

The method returns false, as the input string contains digits and alphabets.

Output

The output of the code will be as follows:


The output of the method NumberUtils.isDigits(23356) is true
The output of the method NumberUtils.isDigits(233sdf) is false
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Attributions:
  1. undefined by undefined