Java program to check if a character is Alphabet or not:
In this post, we will learn how to write a Java program to check if a given character is an Alphabet or not. We will learn different ways to check for an Alphabet in uppercase or lowercase format.
Method 1: Java program to find if a character is an Alphabet by comparing ASCII values:
The ASCII value of a is 97 and the ASCII value of z is 122. The ASCII values of all other small case alphabets fall between these values. Similarly, the ASCII value of A is 65 and the ASCII value of Z is 90. Similar to the lowercase characters, the ASCII value of uppercase alphabets falls in between these two values.
We can compare a character directly with these values to check if it is an Alphabet or not.
Let’s write down the Java program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if ((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90)) {
System.out.println(ch + " is an alphabet.");
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
In this program, we are using a Scanner object to read the user-input character. It assigns that character to the character variable ch and compares its value with the upper and lower ASCII levels for alphabets. Based on the comparison result, it prints a message to the user.
If you run this program, it will ask the user to enter a character and it will print if the entered character is an alphabet or not.
This program will print output as below:
Enter a character:
d
d is an alphabet.
Enter a character:
#
# is not an alphabet.
Method 2: Java program to find if a character is an Alphabet by comparing characters:
Instead of comparing the ASCII values, we can also compare the user-given character with the uppercase and lowercase characters a, z, A, and Z directly. It will give the same result:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
System.out.println(ch + " is an alphabet.");
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
This is a better approach as the code is more readable than the previous example.
Method 3: By using a separate method:
In this approach, we will use a separate method to check for an alphabet character. This method will take one character as its parameter and it will return one boolean value.
import java.util.Scanner;
public class Main {
private static boolean isAlphabet(char ch) {
return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if (isAlphabet(ch)) {
System.out.println(ch + " is an alphabet.");
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
We created a new method isAlphabet that takes one character as its parameter and returns one boolean value. It returns true if the character is an alphabet and else it returns false.
The if condition is calling this method and based on its return value it prints if the character is an alphabet or not.
It will print similar results.
Method 4: By using Character.isAlphabetic() method:
The Character class of Java provides a method to find alphabet characters. This method is defined as:
public static boolean isAlphabetic(int codePoint)
This is a static method defined in the Character class. It finds if the specific character or unicode code point is a character or not. We can pass a character directly to this method. It will convert it to its unicode. It returns one boolean value.
Let’s change the above program to use Character.isAlphabetic:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a character: ");
char ch = scanner.next().charAt(0);
if (Character.isAlphabetic(ch)) {
System.out.println(ch + " is an alphabet.");
} else {
System.out.println(ch + " is not an alphabet.");
}
}
}
It will give similar results.
You might also like:
- 3 ways to convert System.nanoTime to seconds in Java
- 4 different Java program to find the HCF or GCD of two numbers
- Java ArrayList ensureCapacity method
- Java ArrayList clone method explanation with example
- How to find the product of digits of a number in Java
- 3 ways to find perfect numbers in a range in Java
- How to print a Hollow Rhombus pattern in Java
- Java program to print hollow diamond star pattern