Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the isDigit Java function?
3.
Syntax of isDigit() in Java
3.1.
First Form Syntax
3.1.1.
Parameter
3.1.2.
Return Value 
3.2.
Second Form Syntax: 
3.2.1.
Parameter
3.2.2.
Return Value 
4.
Example of isDigit Java 
4.1.
Code in Java 
4.2.
java
4.2.1.
Output
4.3.
java
4.3.1.
Output
4.4.
Explanation
5.
Frequently Asked Questions
5.1.
How do you know if a character is 0 to 9?
5.2.
How to check if a character is an alphabet in Java?
5.3.
How to check a digit in Java?
5.4.
How to find a char is digit in Java?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

isDigit Java

Author Nikhil Joshi
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

Have you ever written your own function to check whether a character is a digit in Java? Well, do you know that the Character wrapper class in Java provides the isDigit method precisely for that?

isDigit Java

In this article titled ‘isDigit Java’, we will discuss the intricacies of the isDigit function. Let’s start our journey!

Also read, Duck Number in Java and Hashcode Method in Java.

What is the isDigit Java function?

The isDigit java function is provided by the Character wrapper class. It is used to check whether a given character is a digit. Java checks for the value returned by Character.getType(ch). If the value returned is DECIMAL_DIGIT_NUMBER constant, then isDigit java returns true else, false.

Some Unicode ranges containing digits:

  1. ISO-Latin digits (0-9): ‘\u0030’ to ‘\u0039’.
     
  2. Arabic-Indic digits: ‘\u0660’’ to ‘\u0669’.
     
  3. Extended Arabic-Indic digits: ‘\u06F0’ to ‘\u06F9’.
     
  4. Devnagari digits: ‘\u0966’ to ‘\u096F’.
     
  5. Fullwidth digits: ‘\uFF10’ to ‘\uFF19’.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax of isDigit() in Java

The Java isDigit function has two forms:

First Form Syntax

public static boolean isDigit(char ch)


This function takes character ch as input. Then it checks if it is a digit or not. This function does not work for supplementary characters.

Parameter

  • ch: This is the character you want to check if it's a digit or not. It's the input to the function, and you provide a single character (char) as an argument

Return Value 

  • The isDigit function returns a boolean value, which means it can be either true or false
     
  • If the provided character ch is a digit (0-9), the function returns true
     
  • If the character ch is not a digit (i.e., it's any other character), the function returns false

Second Form Syntax: 

public static boolean isDigit(int codePoint)


The function takes an integer “codepoint” as the parameter. This function can work for supplementary characters. Supplementary characters are characters of languages like Japanese and Chinese that can not be represented in Unicode-16.

Parameter

  • codePoint: This parameter represents an integer value that corresponds to a Unicode code point. A Unicode code point is a unique number assigned to each character in the Unicode standard. In this form of the isDigit method, you provide an integer value (code point) as the argument

Return Value 

  • Like the first form, the second form of the isDigit function also returns a boolean value (true or false)
     
  • If the specified codePoint corresponds to a digit character in the Unicode standard (0-9), the function returns true
     
  • If the codePoint does not represent a digit character, it returns false

Example of isDigit Java 

1. The following example demonstrates how we can use the isDigit Java function to find if a character is a digit.

Code in Java 

  • java

java

public class IsDigitExample {
   public static void main(String[] args) {
       // demonstration of isDigit java function
      
       // characters storing different values
       char n = '9';
       char a = 'a';
       char b = '*';
       char nn = '4';


       // checking if they are digit or not
       boolean result_n = Character.isDigit(n);
       boolean result_a = Character.isDigit(a);
       boolean result_b = Character.isDigit(b);
       boolean result_nn = Character.isDigit(nn);


       System.out.println("char "+n+" is a digit : "+result_n);
       System.out.println("char "+a+" is a digit : "+result_a);
       System.out.println("char "+b+" is a digit : "+result_b);
       System.out.println("char "+nn+" is a digit : "+result_nn);
   }
}

 

Output

output


2. The following example shows how we can use the isDigit Java function to test if a given password contains both alphabet and numeric characters, i.e. the password is alphanumeric.

  • java

java

public class IsDigitExample {
   public static void main(String[] args) {
       // demonstration of isDigit Java  function used for testing if the password is alphanumeric or not


       // password string
       String password = "hsXsS97RrjhlLfm";


       // countDigits store num of digits present in the password
       int countDigits = 0;


       int passwordLength = password.length(); // password length


       for(int i=0; i<passwordLength; i++){
           char currentCharacter = password.charAt(i);


           // checking if current character is digit
           if(Character.isDigit(currentCharacter)){
               countDigits++;
           }
       }


       // if the number of digit present in the password string is equal to the password string length
       //That means it does not contain any alphabet
       boolean isAlphaNumeric = (countDigits != passwordLength);


       System.out.println("Password is : "+password);
       System.out.println("No of digits in password : "+countDigits);
       System.out.println("No of characters other than digits in password : "+(passwordLength-countDigits));
       System.out.println("Given password is : "+ ((isAlphaNumeric)? "Alphanumerical" : "All digits"));
   }
}

 

Output

Output

Explanation

We are counting the number of digits present in the password string. If the number of digits is equal to the length of the password string, then the password does not contain any extra characters and hence is not alphanumeric. We are looping over the password character by character to count the number of digits.

Also see, Swap Function in Java

Frequently Asked Questions

How do you know if a character is 0 to 9?

To determine if a character is in the range of '0' to '9' (digits), you can use the condition (ch >= '0' && ch <= '9'). If ch falls within this range in the ASCII or Unicode character set, it represents a digit.

How to check if a character is an alphabet in Java?

Similar to the isDigit function in Java, we also have the isAlphabetic function available. This function can classify whether a given character is an alphabet.

How to check a digit in Java?

To check if a character is a digit in Java, you can use the Character.isDigit(char ch) method, which returns a boolean value true if ch is a digit (0-9).

How to find a char is digit in Java?

To find if a character is a digit in Java, use Character.isDigit(char ch). It returns true if ch is a digit (0-9).

Conclusion

In conclusion, the isDigit Java function, provided by the Character wrapper class, can classify a character as a digit. The function takes a character as the parameter and checks if it is a digit. Another function overloads the isDigit function, which takes an integer as the parameter. This overloading is done to classify supplementary characters, which are generally not used but are part of languages like Japanese and Chinese.

Following are some references for further readings in Java:

  1. Input method in Java 
  2. Java Hello World Program 
  3. Difference between While and Do While Loops
     
Previous article
Java Lambda Expressions
Next article
Converting Long to String in Java
Live masterclass