Dremendo Tag Line

Check character is alphabet in Java

if else - Question 11

In this question, we will see how to check if a character is an alphabet or not in Java programming using the if else statement. To know more about if else statement click on the if else statement lesson.

Q11) Write a program in Java to input a character and check if it is an alphabet or not. Make use of logical operators (&& ||) to solve this question.

Program

import java.util.Scanner;

public class Q11
{
    public static void main(String args[])
    {
        char x;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a character ");
        x=sc.nextLine().charAt(0);
        if((x>='A' && x<='Z') || (x>='a' && x<='z'))
        {
            System.out.print("Its an alphabet");
        }
        else
        {
            System.out.print("Its not an alphabet");
        }
    }
}

Output

Enter a character G
Its an alphabet
video-poster