Java Program To Check Whether a Character is Alphabet or not
All the character variables hold an ASCII value for computer usage. This value can be used to check whether the character is an alphabet or not.
Here, in this program, we are given a character and our task is to check whether the given character is an alphabet or not.
Input: Enter the element: R
Output: It is an alphabet.
The above problem can be solved in the following ways:
Method 1: Using if-else statements
Method 2: Using the ternary operator
Method 3: Using ASCII value
Method 4: Using isAlphabetic() method
Let us take a look at each of these methods separately.
Program 1: To Check Whether the Character is Alphabet or Not
In this method, we will use the if-else statement to check whether the given character is an alphabet or not.
Algorithm:
- Start.
- Declare a variable.
- Initialize it.
- Use the if-else statement to check whether the given character is an alphabet or not.
- Display the result.
- Stop.
Below is the code for the same.
The below program demonstrates how to use the if-else statement to check whether the given character is an alphabet or not.
//Java Program to check whether the given character is an alphabet or not
import java.util.Scanner;
public class CheckAlphabet
{
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
char ch; //Declare a character
System.out.println("Enter the character ");
ch=sc.next().charAt(0); //Initialize the character
//check whether alphabet or not using if-else statement
if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))
{
System.out.print(ch+" is an Alphabet ");
}
else
{
System.out.print(ch+" is not an Alphabet ");
}
}
}
Enter the character B
B is an Alphabet
Program 2: To Check Whether the Character is Alphabet or Not
In this method, we will use the ternary operator to check whether the given character is an alphabet or not.
Algorithm:
- Start.
- Declare a variable.
- Initialize it.
- Use the ternary operator to check whether the given character is an alphabet or not.
- Display the result.
- Stop.
Below is the code for the same.
The below program demonstrates how to use the ternary operator to check whether the given character is alphabet or not
//Java Program to check whether the given character is an alphabet or not
import java.util.Scanner;
public class CheckAlphabet
{
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
char ch; //Declare a character
System.out.println("Enter the character ");
ch=sc.next().charAt(0); //Initialize the character
//check whether alphabet or not using if-else statement
String res = (ch>= 'a' && ch<= 'z') || (ch>= 'A' && ch<= 'Z')
? ch+ " is an alphabet."
: ch+ " is not an alphabet.";
System.out.println(res);
}
}
Enter the character d
d is an alphabet.
Program 3: To Check Whether the Character is Alphabet or Not
In this method, we will use the ASCII value to check whether the given character is an alphabet or not. ASCII value is represented by integer values between 0 to 127. The ASCII value of lowercase Alphabets are from 97 to 122 and The ASCII value of uppercase Alphabets are from 65 to 90
Algorithm:
- Start.
- Declare a variable.
- Initialize it.
- The ASCII value of the entered character is checked.
- If it lies between 97 - 122 or 65 - 90 then, it is an alphabet.
- Display the result.
- Stop.
Below is the code for the same.
The below program demonstrates how to use ASCII value to check whether the given character is an alphabet or not. Firstly, the character is initialized. and then, its value is compared to the required condition. If the condition satisfies then, it is an alphabet else it is not.
//Java Program to check whether the given character is an alphabet or not
import java.util.Scanner;
public class CheckAlphabet
{
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
char ch; //Declare a character
System.out.println("Enter the character ");
ch=sc.next().charAt(0); //Initialize the character
//check whether alphabet or not using if-else statement
if((ch>=97 && ch<=122)||(ch>=65 && ch<=90))
{
System.out.print(ch+" is an Alphabet");
}
else
{
System.out.print(ch+" is not an Alphabet");
}
}
}
Enter the character 9
9 is not an Alphabet
Program 4: To Check Whether the Character is Alphabet or Not
In this method, we will use isAlphabetic() method to check whether the given character is an alphabet or not.
Algorithm:
- Start.
- Declare a variable.
- Initialize it.
- Use the isAlphabetic() method to check whether the given character is an alphabet or not.
- Display the result.
- Stop.
Below is the code for the same.
The below program demonstrates how to use isAlphabetic() method to check whether the given character is alphabet or not
//Java Program to check whether the given character is an alphabet or not
import java.util.Scanner;
public class CheckAlphabet
{
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
char ch; //Declare a character
System.out.println("Enter the character ");
ch=sc.next().charAt(0); //Initialize the character
//check whether alphabet or not using if-else statement
if (Character.isAlphabetic(ch))
{
System.out.println(ch + " is an alphabet.");
}
else
{
System.out.println(ch + " is not an alphabet.");
}
}
}
Enter the character 89
89 is not an alphabet.