How to check if a character is a letter/number in Java

In this post, I will be sharing how to check if a character is a letter/number in Java. There are two ways to achieve our goal using built-in Java methods:

1. Using isDigit() method

2. Using isLetter() method

Read Also:   Get the first character of a string in Java

Let's dive deep into the topic:

Check if a character is a letter/number in Java

Both isDigit() and isLetter() methods belongs to the Character class in Java.

1. Using isDigit() method


We can easily check if a character in a given alphanumeric string is a letter or number by using the isDigit() method. It is a static method and determines whether the specified character is a digit or not. The syntax of the isDigit method is given below:

 public static boolean isDigit(char ch)


Java Program using isDigit() method


First, we will start by creating a string containing alphanumeric characters. Then, we will iterate through the characters of the given string using for loop and get the value at the specific location using the charAt() method. The last step is to pass the character value as an argument to the isDigit() method.

 public class CheckCharacterDigitOrLetter {
    public static void main(String args[]) {
        // Given String    
        String str = "1Awe2some3";
        // Using for loop
        for (int i =0; i < str.length(); i++)
        {
            char ch = str.charAt(i);
            /* Checking if the character is digit and storing the 
            boolean value in variable result */
            Boolean result = Character.isDigit(ch);
            if (result)
            {
                System.out.println(" '" + ch + "' is a digit");
            }    
            else 
            {
                System.out.println(" '" + ch + "' is a letter");   
            }
        }
    }
}


Output:
'1' is a digit
'A' is a letter
'w' is a letter
'e' is a letter
'2' is a digit
's' is a letter
'o' is a letter
'm' is a letter
'e' is a letter
'3' is a digit


2. Using isLetter() method


We can easily check if a character in a given alphanumeric string is a letter or number by using the isLetter() method. It is a static method and determines whether the specified character is a letter or not. The syntax of the isLetter method is given below:

 public static boolean isLetter(char ch)


Java Program using isLetter() method


Given a string containing alphanumeric characters. Iterate through the given string using for loop and determine the value at the specified location using the charAt() method. To check if a character value is a digit or letter, pass it as an argument to the isLetter() method.

 public class CheckCharacterDigitOrLetter2 {
    public static void main(String args[]) {
        // Given String    
        String str = "1Per2fect3";
        // Using for loop
        for (int i =0; i < str.length(); i++)
        {
            char ch = str.charAt(i);
            /* Checking if the character is letter and storing the 
            boolean value in variable result */
            Boolean result = Character.isLetter(ch);
            if (result)
            {
                System.out.println(" '" + ch + "' is a letter");
            }    
            else 
            {
                System.out.println(" '" + ch + "' is a digit");   
            }
        }
    }
}


Output:
'1' is a digit
'P' is a letter
'e' is a letter
'r' is a letter
'2' is a digit
'f' is a letter
'e' is a letter
'c' is a letter
't' is a letter
'3' is a digit


That's all for today. Please mention in the comments in case you have any questions related to how to check if a character is a letter/number in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry