Java Programs to Check Character is Alphabet or Not

In the previous article we have discussed about Java Program to Print Alphabets from A to Z

In this article we are going to see how to check the input character is an alphabet or not using Java

Java Programs to Check Character is Alphabet or Not

We have many characters including all alphabets, digits and special characters. So in this program we are going to check a character is alphabet or not.

Let’s see different ways to solve it.

Method-1: Java Programs to Check Character is Alphabet or Not By Using If-Else Statement and ASCII Value

Approach:

  • Take a character variable as ch.
  • Prompt the user to enter the character as input value.
  • After getting the value in ch, put it into a if condition where it will be checked that the value of ch is in between 65-90(ASCII value from A-Z) or the value of ch is in between 97-122(ASCII value from a-z). We don’t have to convert the character to its corresponding ASCII value here.
  • If the IF condition satisfied the print that character is an alphabet, if not then the else block will be printed that the character is not an alphabet.

Program:

import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //create object of scanner class
        Scanner inp= new Scanner(System.in);
        System.out.print("Enter the Character: ");
        //take input from user
        char ch=inp.next().charAt(0);
        //check the condition.
        if((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
        {
            System.out.println(ch+" is an Alphabet.");
        }
        else
        {
            System.out.println(ch+" is not an Alphabet.");
        }
    }
}
Output:

Enter the Character: 5
5 is not an Alphabet.

Method-2: Java Programs to Check Character is Alphabet or Not By Using If-Else Statement and Alphabet

Approach:

  • Take a character variable as ch.
  • Prompt the user to Enter the character as input value.
  • After getting the value in ch, put it into a condition where it will be checked that the value of ch is in character value from “A” to “Z” or the value of ch is in character value from “a” to “z”.
  • If the IF condition satisfied the print that character is an alphabet, if not then the else block will be printed that the character is not an alphabet.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        Scanner inp= new Scanner(System.in);
        //create object of scanner class
        System.out.print("Enter the Character: ");
        //take input from user
        char ch=inp.next().charAt(0);
        //check the condition.
        if((ch>='A' && ch<='B') || (ch>='a' && ch<='z'))
        {
            System.out.println(ch+" is an Alphabet.");
        }
        else
        {
            System.out.println(ch+" is not an Alphabet.");
        }
    }
}
Output:

Enter the Character: f
f is an Alphabet.

Method-3: Java Programs to Check Character is Alphabet or Not By Using Ternary Operator

Approach:

  • Take a character variable as ch.
  • Prompt the user to enter a character as input value.
  • Declare another string variable named as res.
  • Assigned the value to it in the form of a condition so that it contain a Boolean value.
  • If the Boolean value is true then the whole line followed by a “?” symbol will be assigned to res variable, if the Boolean value is false then the whole line followed by “:” symbol will be assigned to res variable.
  • Finally print the res.

Program:

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args)
    {
        //create object of scanner class
        Scanner inp= new Scanner(System.in);
        System.out.print("Enter the Character: ");
        //take input from user
        char ch=inp.next().charAt(0);
        //check the condition.
        String res=((ch>='A' && ch<='B') || (ch>='a' && ch<='z'))
        //if the above condition true then the value is assigned to res
        ? ch +" is an Alphabet."
        //if the above condition false then the value is assigned to res
        : ch +" is not an alphabet.";
        System.out.print(res);
    }
}
Output:

Enter the Character: ?
? is not an alphabet.

Method-4:

Approach:

  • Take the input from the user as ch.
  • Prompt the user to enter a character.
  • There is a predefined method available that is isAlphabetic(), which can check whether the character is an alphabet or not.
  • It also returns the Boolean value so we put it into a IF block.
  • If the statement has true value, then The IF block value will be printed that is the character is an alphabet otherwise the else block will be printed that is the character is not an alphabet.

Program:

import java.util.Scanner;
class Main 
{
    public static void main(String[] args) 
    {
        //create object of scanner class
        Scanner inp= new Scanner(System.in);
        System.out.print("Enter the Character: ");
        //take input from user
        char ch=inp.next().charAt(0);
        //check the condition.
        if (Character.isAlphabetic(ch)) 
        {
            System.out.println(ch + " is an alphabet.");
        }
   		else 
   		{
            System.out.println(ch + " is not an alphabet.");
  		}
  	}
}
Output:

Enter the Character: 0
0 is not an alphabet.

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: