Java

How to Check if a Character is a Number in Java

Java comprises a class named Character, which belongs to java.lang package. The object of the Character class contains a single field “char” that is a primitive datatype in Java. A Java char has a 16-bit size, with a range of 0 to 65,536 characters. The data type char belongs to the characters group, a character set representing symbols such as alphabets and numerals.

This guide will discuss the method of checking if the specified character is a number or not.

How to Check if a Character is a Number in Java?

The Java built-in “isDigit()” method of the Character Class can be utilized for validating a character, whether it is a number or not.

Syntax

isDigit(char ch)

This method takes a character “ch” input as an argument and returns a boolean value, where “true” indicates that the given value is a digit and “false” signifies it is not.

Now, head toward the below-given examples for a better understanding.

Example 1

In this example, we have two characters “a” and “b” with “F” and “5” as their values:

char a='F';
char b='5';

We will call the method “isDigit()” of the Character Class in a print statement and pass the created characters to it as arguments. This method will check whether the character is a digit or not and returns the corresponding boolean value:

System.out.println("The " + a + " is a digit? " + Character.isDigit(a));
System.out.println("The " + b + " is a digit? "+ Character.isDigit(b));

The output indicates that the “F” character is not a digit and “5” is a digit as it returns “true”:

Example 2

As we know the character type represents symbols such as alphabets and numerals. So in the below example, we have three char-type variables, assigned with two characters and one numeric value:

char a='7';
char b= 65;
char c='9';

Here, we call the “isDigit()” method in the “System.out.println()” and pass the created characters as arguments to it:

System.out.println("The " + a + " is a digit? " + Character.isDigit(a));
System.out.println("The " + b + " is a digit? "+ Character.isDigit(b));
System.out.println("The " + c + " is a digit? "+ Character.isDigit(c));

The output states that 7 and 9 are digits because the method returned a “true” value while the 65 that is stored in the char variable “b” is not a digit. Now, why does the “false” value is returned for “b” char? Because 65 is the ASCII representation of “A”, the “isDigit()” method checked if “A” is a number or not and returned “false”, considering it not a number:

Example 3

In this example, we will print a statement indicating that the given character is a digit or not. Here, we have a char type variable “a” that has a character value “2”:

char a='2';

Here, we will use the “if” condition to check whether the character is a digit or not, and then the related statement on screen:

 if(Character.isDigit(a))
    {
        System.out.println(a + " is a digit");
    }
    else
    {
        System.out.println(a + " is not a digit");
  }

The output shows “2” is a digit:

We have compiled the necessary information related to checking if a character is a number or not in Java.

Conclusion

You can use the Java built-in “isDigit()” method of the Character Class to validate whether a character is a number. It determines whether the given character is a digit or not and returns boolean values: “true” or “false”. If its argument is a character, it will return true; otherwise, the returned case will be set as false. This guide discussed the methods for checking if a character is a number.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.