isDigit() Method in Java

Rumman Ansari   Software Engineer   2019-03-30   9839 Share
☰ Table of Contents

Table of Content:


Description

The Character.isDigit(char ch) java method determines if the specified character is a digit.

 

A character is a digit if its general category type, provided by Character.getType(ch), is DECIMAL_DIGIT_NUMBER.

Some Unicode character ranges that contain digits:

  • ‘\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

Many other character ranges contain digits as well.

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the isDigit(int) method.

The isDigit(char ch) method of Character class is static thus it should be accessed statically which means the we would be calling this method in this format:

Character.isDigit(char ch)

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java isDigit() method non statically.

Method Syntax

public static boolean isDigit(char ch)

Method Argument

Data Type Parameter Description
char ch the character to be tested. Primitive character type.

Method Returns

This method returns true, if the passed character is really a digit.

Compatibility

Requires Java 1.0 and up

Example

Below is a simple java example on the usage of isDigit(char ch) method of Character class.

public class MethodIsDigit {

   public static void main(String args[]) {
      System.out.println(Character.isDigit('s'));
      System.out.println(Character.isDigit('2'));
   }
}

output

This will produce the following result ?

false
true
Press any key to continue . . .

Example

Below is a simple java example on the usage of isDigit(char ch) method of Character class.



import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * isDigit(char ch) method of Character class.
 */

public class CharacterIsDigitCharExample {

	public static void main(String[] args) {


		// Ask for user input
		System.out.print("Enter a character:");

		// use scanner to get the user input
		Scanner s = new Scanner(System.in);

		// get a single character
		char value = s.nextLine().toCharArray()[0];

		// close the scanner object
		s.close();

		// check if the user input is digit or not
		boolean checkBool = Character.isDigit(value);
		// print result
		if(checkBool){
			System.out.println("User input \'"+value+"\' is a digit");
		}
		else{
			System.out.println("User input \'"+value+"\' is not a digit");
		}

	}

}

output

This will produce the following result ?

Enter a character:2
User input '2' is a digit
Press any key to continue . . .

Enter a character:s
User input 's' is not a digit
Press any key to continue . . .