Java Program to Check Whether a Character is Alphabet or Not

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Program to Check Whether a Character is Alphabet or Not

In this article we will see how we can check whether a character is alphabet or not. So before going into the actual topic, let’s know about alphabet first.

Alphabet:

In English language there are 26 letters which are called English alphabets.

  1. Capital Letters: A to Z
  2. Small Letters: a to z

There are several ways in which we can check and see if an entered character is an alphabet or not.

Method-1 : Using if-else

To check whether the character is an alphabet or not we can use if-else statement.

Approach:

  • Take one character input from user.
  • Check if the character falls under the alphabets’ range.
  • If it falls under the alphabet’s range mentioned in if condition then it is an alphabet.
  • Otherwise it is not an alphabet.

Program:

import java.util.Scanner;
class checkAlphabet
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a character");
        //Taking the character as input from the user
        char ch = scan.next().charAt(0);
        //CHecking wheteher the character comes under alphabets
        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:

//CASE-1
Enter a character
f
f is an alphabet

// CASE-2
Enter a character
3
3 is not an Alphabet

Method-2 : Using ASCII

To check whether the character is an alphabet or not we can use ASCII values.

Approach:

  • Like the previous example we can do the same thing however rather than using the alphabetical range, we will be using the ASCII values. ASCII value is the integer value allotted to each character.

Program:

import java.util.Scanner;
class checkAlphabet{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a character");
        //Taking the character as input from the user
        char ch = scan.next().charAt(0);
        //Checking wheteher the character comes under alphabets using ASCII
        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 ");
        }
    }
}
Output:

//CASE-1
Enter a character
f
f is an alphabet

// CASE-2
Enter a character
3
3 is not an Alphabet

Method-3 : Using isAlphabetic() method

To check whether the character is an alphabet or not we can use in-built function isAlphabetic( ).

Approach:

  • Take the character user input.
  • Pass it to isAlphabetic() method.
  • This method will check it is an alphabet or not.

Program:

import java.util.Scanner;
class checkAlphabet
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a character");
        //Taking the character as input from the user
        char ch = scan.next().charAt(0);
        //CHecking wheteher the character comes under alphabets using library function
        if(Character.isAlphabetic(ch))
        {
            System.out.print(ch+" is an alphabet ");
        }
        else
        {
            System.out.print(ch+" is not an Alphabet ");
        }
    }
}
Output: 

//CASE-1 
Enter a character 
f 
f is an alphabet 

// CASE-2 
Enter a character 
3 
3 is not an Alphabet

Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.

Related Java Decision Making and Loop Program: