Java Character isLetterOrDigit Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the isLetterOrDigit(char ch) method of the Character class in Java, which is used to check whether the specified character is a letter or a digit. We will learn how to use this method through different examples.

Add class and main method

Add the LetterOrDigit class and the main method to the LetterOrDigit.java file.

public class LetterOrDigit {
    public static void main(String[] args) {

    }
}

Check if a character is a letter or a digit

Inside the main method, define a char variable and initialize it with a character of your choice. Then, use the isLetterOrDigit(char ch) method of the Character class to check if the character is a letter or a digit. Print the result to the console.

public class LetterOrDigit {
    public static void main(String[] args) {
        char ch = 'A';
        boolean letterOrDigit = Character.isLetterOrDigit(ch);
        System.out.println("Is " + ch + " a letter or a digit? " + letterOrDigit);
    }
}

Save the file and run this command in the terminal to compile and run the file:

javac LetterOrDigit.java && java LetterOrDigit

Check if multiple characters are letters or digits

Now, define multiple char variables and initialize them with different characters. Then, use a loop to iterate over all the characters and use the isLetterOrDigit(char ch) method to check if each character is a letter or a digit. Print the result for each character to the console.

public class LetterOrDigit {
    public static void main(String[] args) {
        char ch1 = 'A';
        char ch2 = '5';
        char ch3 = ':';
        char ch4 = 'd';
        char ch5 = '%';

        char[] chars = {ch1, ch2, ch3, ch4, ch5};

        for (char ch : chars) {
            boolean letterOrDigit = Character.isLetterOrDigit(ch);
            System.out.println("Is " + ch + " a letter or a digit? " + letterOrDigit);
        }
    }
}

Save the file and run the same command in the terminal to compile and run the file.

Get input from the user

Now, let's modify the program to get the character input from the user. Use the Scanner class to get a char input from the user and store it in a variable. Then, use the isLetterOrDigit(char ch) method to check if the input character is a letter or a digit. Print the result to the console.

import java.util.Scanner;

public class LetterOrDigit {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = input.next().charAt(0);
        boolean letterOrDigit = Character.isLetterOrDigit(ch);
        System.out.println("Is " + ch + " a letter or a digit? " + letterOrDigit);
    }
}

Save the file and run the same command in the terminal to compile and run the file.

Summary

In this lab, we learned about the isLetterOrDigit(char ch) method of the Character class in Java, which is used to check whether the specified character is a letter or a digit. We learned how to use this method through different examples and practiced reading user input in Java using the Scanner class.

Other Java Tutorials you may like