Home »
Java Programs »
Java Basic Programs
Java program to check whether a given character is alphabet or not
In this java program, we will learn how to check whether a given character in an alphabet or not? Here, we are reading a character from the user and validating it is alphabet or not.
Submitted by IncludeHelp, on December 05, 2017
Given a character and we have to check whether it is an alphabet or not?
Example 1:
Input:
Enter a character: A
Output:
A is an alphabet
Example 2:
Input:
Enter a character: @
Output:
@ is not an alphabet
Program to check given character is an alphabet or not in java
import java.util.Scanner;
public class AlphabetOrNot
{
public static void main(String args[])
{
//create and initialize object.
char ch;
Scanner scan = new Scanner(System.in);
//Input character
System.out.print("Enter a Character : ");
ch = scan.next().charAt(0);
//condition for checking characters.
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");
}
}
}
Output
First run:
Enter a Character : A
A is an Alphabet
Second run:
Enter a Character : @
@ is not an Alphabet
Java Basic Programs »