Do you think IIT Guwahati certified course can help you in your career?
No
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?
In this article titled ‘isDigit Java’, we will discuss the intricacies of the isDigit function. Let’s start our journey!
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:
ISO-Latin digits (0-9): ‘\u0030’ to ‘\u0039’.
Arabic-Indic digits: ‘\u0660’’ to ‘\u0669’.
Extended Arabic-Indic digits: ‘\u06F0’ to ‘\u06F9’.
Devnagari digits: ‘\u0966’ to ‘\u096F’.
Fullwidth digits: ‘\uFF10’ to ‘\uFF19’.
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); } }
You can also try this code with Online Java Compiler
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
// 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")); } }
You can also try this code with Online Java Compiler
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.
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: