Algorithm
-
Input:
- Accept a character as input.
-
Check if the character is an alphabet:
- Use the
Character.isLetter(char ch)
method from theCharacter
class in Java to check if the given character is an alphabet.- If
Character.isLetter(ch)
returnstrue
, then the character is an alphabet. - If
Character.isLetter(ch)
returnsfalse
, then the character is not an alphabet.
- If
- Use the
-
Output:
- Display the result based on the check performed in step 2.
Code Examples
#1 Code Example- Java Program to Check Alphabet using if else
Code -
Java Programming
public class Alphabet {
public static void main(String[] args) {
char c = '*';
if( (c >= 'a' && c <= 'z'> || (c >= 'A' && c < = 'Z'))
System.out.println(c + " is an alphabet.");
else
System.out.println(c + " is not an alphabet.");
}
}
Copy The Code &
Try With Live Editor
Output
* is not an alphabet.
#2 Code Example- Java Program to Check Alphabet using ternary operator
Code -
Java Programming
public class Alphabet {
public static void main(String[] args) {
char c = 'A';
String output = (c >= 'a' && c < = 'z') || (c >= 'A' && c <= 'Z')
? c + " is an alphabet."
: c + " is not an alphabet.";
System.out.println(output);
}
}
Copy The Code &
Try With Live Editor
Output
A is an alphabet.
#3 Code Example - Java Program to Check Alphabet using isAlphabetic() Method
Code -
Java Programming
class Main {
public static void main(String[] args) {
// declare a variable
char c = 'a';
// checks if c is an alphabet
if (Character.isAlphabetic(c)) {
System.out.println(c + " is an alphabet.");
}
else {
System.out.println(c + " is not an alphabet.");
}
}
}
Copy The Code &
Try With Live Editor
Output
a is an alphabet.
Demonstration
Java Programing Example to Check Whether a Character is Alphabet or Not-DevsEnv