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

Java Programming Language String in java (Article) String in java (Program)

43

Given Input:

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

Expected Output:

2 is a digit.

Program:

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.");
        }
    }
}

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.


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.