Enter a character: A
It is a Letter.
Enter a character: 7
It is a Digit.
import java.util.Scanner;
public class CheckCharacter {
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 (Character.isDigit(ch)) {
System.out.println("It is a Digit.");
}
else if (Character.isLetter(ch)) {
System.out.println("It is a Letter.");
}
else {
System.out.println("It is neither a Digit nor a Letter.");
}
sc.close();
}
}
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.