Java program to check a character is an alphabet or not

A character is an alphabet or not using java

A character will be an alphabet,  if and only if it is either in uppercase letter or in lowercase letter.In Java, char data type stores the ASCII value of a character (number between 0 and 127) rather than the character itself.

Lowercase letters( ‘ a ‘ to ‘ z ‘ ) have ASCII values between the range 97 to 122 , whereas Uppercase letters( ‘ A ‘ to ‘ Z ‘ ) have ASCII values between the range 65 to 90.

In this article we will discuss whether a particular character entered by the user is an alphabet or not using java programming language.

 
 
A character is an alphabet or not using java

Working:-

Step 1 : Ask the user to input a character.

Step 2 : Now check the input character should either comes in the range of ( ‘ A ‘ to’ Z ‘ )  or ( ‘ a ‘ to’ z ‘ ).

Step 3 : If above step(Step : 2) gets true, then the character entered by the user is an alphabet but, if vice -versa happened, means if the above step(Step : 2) gets false then the character is not an alphabet.

Step 4 : Print the result.

Java Code:-

//Java program to check whether the character entered by the user is an alphabet or not.
import java.util.Scanner;
public class alphabetornot
{										//class declaration
    public static void main(String[] args)
    {										//main method declaration
        char c;
        Scanner sc = new Scanner(System.in);

	System.out.print("Enter a Character : ");                       //Input character
        c = sc.next().charAt(0);
										//condition for checking characters
        if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
			System.out.println(c + " is an Alphabet");
		else
			System.out.println(c + " is not an Alphabet");
		sc.close();			                               //closing scanner class(not compulsory, but good practice)
	}                                                                      //end of the main method
}                                                                              //end of the class
Output:
Enter a Character : h
h is an Alphabet

Enter a Character : 8
8 is not an Alphabet

Enter a Character : @
@ is not an Alphabet

One comment on “Java program to check a character is an alphabet or not”


  • Shubham

    public class JavaApplication1
    {
    public static void main(String[] args)
    {
    Scanner obj = new Scanner(System.in);
    char c = obj.next().charAt(0);
    if(c>=’a’&&c<='z')
    System.out.print("Yes");
    else
    System.out.println("No");
    }

    }