Java Program to Check Alphabet

This post covers a program in Java that checks whether a character entered by user at run-time of the program, is an alphabet or not.

Note - All characters from either A-Z or a-z are alphabets.

Check Alphabet using if-else in Java

The question is, write a Java program to check whether a character is an alphabet or not using if-else. The program given below is its answer:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      char ch;
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter a Character: ");
      ch = s.next().charAt(0);
      
      if((ch>='A' && ch<='z') || (ch>='a' && ch<='z'))
      {
         System.out.println("\nIt is an Alphabet.");
      }
      else
      {
         System.out.println("\nIt is not an Alphabet.");
      }
   }
}

Here is its sample run with user input c as character to check whether it is an alphabet or not:

java program to check alphabet

The above program can also be written as:

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter a Character: ");
      char ch = s.next().charAt(0);
      
      if((ch>='A' && ch<='z') || (ch>='a' && ch<='z'))
         System.out.println("\nThe character \'" +ch+ "\' is an Alphabet.");
      else
         System.out.println("\nThe character \'" +ch+ "\' is not an Alphabet.");
   }
}

The sample run with user input 4 is shown in the snapshot given below:

java check alphabet using if else

Check Alphabet or Digit in Java

This program checks whether a given character is an alphabet, a digit, or any other character.

import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      Scanner s = new Scanner(System.in);
      
      System.out.print("Enter a Character: ");
      char ch = s.next().charAt(0);
      
      if((ch>='A' && ch<='z') || (ch>='a' && ch<='z'))
         System.out.println("\nThe character \'" +ch+ "\' is an Alphabet.");
      else if(ch>='0' && ch<='9')
         System.out.println("\nThe character \'" +ch+ "\' is a Digit.");
      else
         System.out.println("\nThe character \'" +ch+ "\' is neither an Alphabet nor a Digit.");
   }
}

The sample run with same user input as of previous program's sample run is:

java check alphabet or digit

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋