Exercise:
Write a Java Program to check whether a character is an alphabet character or not.
1. Check using if..else statement.
Click Here to View the Solution!
public
class AlphabetChecker {
public static void main(String[] args) {
char ch =
'-';
if( (ch >=
'a' && ch <=
'z') || (ch >=
'A' && ch <=
'Z'))
System.out.println(ch +
" is an alphabet character.");
else
System.out.println(ch +
" is not an alphabet character.");
}
}
Click Here to View the Output!
- is not an alphabet character.
Click Here to View the Explanation!
- This program is used for finding that whether the character stored in a variable is an alphabet or not by using the if…else statement and display the result on the screen.
- A character variable
ch
is used for storing the value in the form of an ASCII value of a character instead of the character itself. - It is to be noted that the range of the lowercase alphabets in ASCII is from 97 to 122 while that of uppercase alphabets is from 65 to 90. This means that 97 is a and 122 is z while 65 is A and 90 is Z.
- The character “-” is stored in
ch
. It is then compared to the ASCII values ranging between a to z and A to Z. That is if on comparison, the ASCII value of the character variablech
matches any ASCII value between 97 to 122 and 65 to 90, it will be considered an alphabet. - Since the ASCII value of “-” does not match any ASCII value of the alphabets, the result “- is not an alphabet character” will be displayed on the screen.
2. Check using Ternary Operator.
Click Here to View the Solution!
public
class Alphabet {
public static void main(String[] args) {
char ch =
'b';
String output = (ch >=
'a' && ch <=
'z') || (ch >=
'A' && ch <=
'Z')
? ch +
" is an alphabet character."
: ch +
" is not an alphabet character.";
System.out.println(output);
}
}
Click Here to View the Output!
b is an alphabet character.
Click Here to View the Explanation!
- This program performs the process of the identification of an alphabet by using a ternary operator and displays the result on the screen.
- This program is similar to the above program, except that instead of using the if…else statement, this program uses the ternary operator
(?:)
, which functions in the same way as an if…else statement. - If the character “b” is stored in the character variable
ch
, then on comparison, the program will print the result “b is an alphabet character”.
3. Check using isAlphabetic() Method.
Click Here to View the Solution!
public class AlphabetChecker {
public static void main(String[] args) {
char ch =
'h';
// built in method to check if ch is an alphabet
if (Character.isAlphabetic(ch)) {
System.out.println(ch +
" is an alphabet character.");
}
else {
System.out.println(ch +
" is not an alphabet character.");
}
}
}
Click Here to View the Output!
h is an alphabet character.
Click Here to View the Explanation!
- This program identifies an alphabet stored in a character variable by using a Java method,
isAlphabetic()
and the if…else statement and displays the result. - A character “
h
” is stored in the character variablech
. TheisAlphabetic()
method is stated in the if statement as an expression of a condition which belong to the Character Class of Java. The expression used is(Character.isAlphabetic(ch))
. - The
isAlphabetic()
method functions in a simple way. It returns true if the variable it reads contains an alphabet else returns false and prints the output. - Since “
h
” is an alphabet, theisAlphabetic()
method will return true and print “h is an alphabet character”.