classify a character as a letter, digit, or special symbol using Java
classify a character as a letter, digit, or special symbol using Java
classify a character as a letter, digit, or special symbol using Java
char ch = '2'; // Change this to test different characters
2 is a digit.
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.");
}
}
}
2 is a digit.
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:
ch is 'a', the output will be: a is a letter.ch is '5', the output will be: 5 is a digit.ch is '@', the output will be: @ is a special symbol.You can test different characters by changing the value of ch in the code.
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.
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.