Home / Programs / classify a character as a letter, digit, or special symbol using Java
🚀 Programming Example

classify a character as a letter, digit, or special symbol using Java

👁 131 Views
💻 Practical Program
📘 Step Learning

classify a character as a letter, digit, or special symbol using Java

📌 Information & Algorithm

Given Input:

  char ch = '2'; // Change this to test different characters

Expected Output:

2 is a digit.

💻 Program Code

public class Main {
    public static void main(String[] args) {
        char ch = '2'; // Change this to test different characters

        if (Character.isLetter(ch)) {
            System.out.println(ch + " is a letter.");
        } else if (Character.isDigit(ch)) {
            System.out.println(ch + " is a digit.");
        } else {
            System.out.println(ch + " is a special symbol.");
        }
    }
}

                        

🖥 Program Output

2 is a digit.
                            

📘 Explanation

This code uses the Character.isLetter(char ch) method to check if the character is a letter, the Character.isDigit(char ch) method to check if the character is a digit, and the else clause to handle special symbols.

Here are some examples of how the output will look:

  • If ch is 'a', the output will be: a is a letter.
  • If ch is '5', the output will be: 5 is a digit.
  • If ch is '@', the output will be: @ is a special symbol.

You can test different characters by changing the value of ch in the code.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.