LAST UPDATED: OCTOBER 20, 2020
Java Character isDigit(int codePoint) Method
isDigit(int codePoint)
method is a part of Character
class. This method checks whether the specified Unicode codepoint character is a digit or not. A character is defined as a digit if its general category type provided by Character.getType(ch)
is a DECIMAL_DIGIT_NUMBER
.
Some Unicode ranges that comprise the digits are:
'\u0030'
through '\u0039'
, ISO-LATIN-1 digits ('0'
through '9'
)
'\u0660'
through '\u0669'
, Arabic-Indic digits.
'\u06F0'
through '\u06F9'
, Extended Arabic-Indic digits.
'\u0966'
through '\u096F'
, Devanagari digits.
'\uFF10'
through '\uFF19'
, Fullwidth digits.
Syntax:
public static boolean isDigit(int codePoint)
Parameter:
The parameter passed is the Unicode codepoint character value to be checked whether it is a digit or not.
Returns:
Returns the boolean value true
if the specified character is a digit else return false
.
Example 1:
Here, the codepoints are checked by using the isDigit()
method whether they are digits or not.
public class StudyTonight
{
public static void main(String[] args)
{
int cp1 = 88;
int cp2 = 34;
int cp3 = 50;
int cp4 = 83;
int cp5 = 55;
boolean b1 = Character.isDigit(cp1);
boolean b2 = Character.isDigit(cp2);
boolean b3 = Character.isDigit(cp3);
boolean b4 = Character.isDigit(cp4);
boolean b5 = Character.isDigit(cp5);
System.out.println((char)cp1 +" is a digit??: "+b1);
System.out.println((char)cp2 +" is a digit??: "+b2);
System.out.println((char)cp3 +" is a digit??: "+b3);
System.out.println((char)cp4 +" is a digit?? : "+b4);
System.out.println((char)cp5 +" is a digit??: "+b5);
}
}
X is a digit??: false
" is a digit??: false
2 is a digit??: true
S is a digit?? : false
7 is a digit??: true
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the codepoint: ");
Scanner sc = new Scanner(System.in);
int cp = sc.nextInt();
boolean b = Character.isDigit(cp);
System.out.println((char)cp + " is a digit?? : "+b);
}
catch(Exception e)
{
System.out.println("Invalid Input!!");
}
}
}
Enter the codepoint: 54
6 is a digit?? : true
*****************************
Enter the codepoint: 66
B is a digit?? : false
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.