Write a program to check whether a character is alphabet or not


This Java program checks whether the user-input character is an alphabet or not. Here's a brief explanation of the code:

  • We import the Scanner class to take user input from the console.
  • We define a public static void main method to execute our code.
  • Inside the main method, we create an object of the Scanner class to take user input.
  • We prompt the user to enter a character and store it in a char variable ch.
  • We check if the entered character is within the ASCII range of uppercase and lowercase alphabets using the logical OR || operator and the comparison operators >= and <=.
  • If the entered character is within the ASCII range of uppercase and lowercase alphabets, we print "This is a Alphabet".
  • If the entered character is not within the ASCII range of uppercase and lowercase alphabets, we print "This is a Not Alphabet".

Source Code

import java.util.Scanner;
class Check_Alphabet
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Character :");
		char ch = input.next().charAt(0);
 
		if(ch>='a' && ch<='z' || ch>='A' && ch<='Z')
		{
			System.out.println("This is a Alphabet");
		}
		else
		{
			System.out.println("This is a Not Alphabet");
		}
	}
}

Output

Enter the Character :s
This is a Alphabet

Example Programs