Check Alphabet/Digit/Special Character

Check whether a character is an alphabet, digit, or special character.

BeginnerTopic: Module 2: Conditional Programs
Back

Java Check Alphabet/Digit/Special Character Program

This program helps you to learn the fundamental structure and syntax of Java programming.

Try This Code
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);

        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
            System.out.println(ch + " is an Alphabet");
        } else if (ch >= '0' && ch <= '9') {
            System.out.println(ch + " is a Digit");
        } else {
            System.out.println(ch + " is a Special Character");
        }

        sc.close();
    }
}
Output
Enter a character: 9
9 is a Digit

Understanding Check Alphabet/Digit/Special Character

We use character ranges to classify the input as alphabet, digit, or special symbol.

Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.

Table of Contents