Signup/Sign In
LAST UPDATED: NOVEMBER 6, 2020

Java Character isDigit(char ch) Method

Java isDigit(char ch) method is a part of Character class. This method checks whether the specified 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(char ch)  

Parameter:

The parameter passed is the 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 characters are checked by using the isDigit() method whether they are digits or not.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char ch1 = 'A';  
		char ch2 = 'u';  
		char ch3 = '8';  
		char ch4 = '4';  
		char ch5 = '*';  

		boolean b1 = Character.isDigit(ch1);  
		boolean b2 = Character.isDigit(ch2);  
		boolean b3 = Character.isDigit(ch3);  
		boolean b4 = Character.isDigit(ch4);  
		boolean b5 = Character.isDigit(ch5);  

		System.out.println(ch1 +" is a digit??:  "+b1);  
		System.out.println(ch2 +" is a digit??:  "+b2);  
		System.out.println(ch3 +" is a digit??:  "+b3);  
		System.out.println(ch4 +" is a digit?? : "+b4);  
		System.out.println(ch5 +" is a digit??:  "+b5);  
	}  
}  


A is a digit??: false
u is a digit??: false
8 is a digit??: true
4 is a digit?? : true
* is a digit??: false

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 character: ");  
			Scanner sc = new Scanner(System.in);         
			char ch = sc.next().charAt(0);  
			boolean b = Character.isDigit(ch);
			System.out.println(ch + " is a digit?? : "+b);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter the character: y
y is a digit?? : false
****************************
Enter the character: 2
2 is a digit?? : true

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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.