Java program to check a character is an alphabet or not

A character is an alphabet or not using java

Here, in this section we will discuss the program to check whether the 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 this article we will discuss whether a particular character entered by the user is an alphabet or not using java programming language.

character is an Alphabet or not using java

Explanation

In C programming language a char type variable can store many different types of characters-

  • Alphabets (a, b, c… )
  • Digits(1, 2, 3…)
  • Special characters(@, %, &…)

These characters are differentiated on the basis of ASCII values :

  • between 65 and 90 for upper case(A, B, C…)
  • between 97 and 122 for lower case(a, b, c…)

In here we will see how to identify whether a character is alphabet or not using C++ programming language.

Working

  • Get user input
  • Check if input is between ‘A'(65) – ‘Z'(90) or between ‘a'(96) – ‘z'(122)
  • If True print ‘Yes’
  • If False print ‘No’
Character is an alphabet or not

Java code (method 1)

Run

 

//Java program to check whether the character entered by the user is an alphabet or not.
import java.util.Scanner;

//class declaration
public class Main
{						
    
    //main method declaration
    public static void main(String[] args)
    {										
        char ch;
                              
        ch = '9';
		
	//condition for checking characters
        if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
	      System.out.println("The inserted character " + ch + " is an Alphabet");
	else
	      System.out.println("The inserted character " + ch + " is not an Alphabet");
		
				                               
	
    } //end of the main method
    
} //end of the class

Output

Insert any character: 9
The entered character 9 is not an Alphabet

You can also check the alphabet using the ASCII values of characters like this:

if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
      System.out.println("The inserted character " + ch + " is an Alphabet");
else
      System.out.println("The inserted character " + ch + " is not an Alphabet");

Java code (method 2)

Run
//Java program to check whether the character entered by the user is an alphabet or not.
import java.util.Scanner;

//class declaration
public class Main
{						
    
    //main method declaration
    public static void main(String[] args)
    {										
        char ch;
                         
        ch = 'k';
		
	//condition for checking characters
        if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
	      System.out.println("The inserted character " + ch + " is an Alphabet");
	else
	      System.out.println("The inserted character " + ch + " is not an Alphabet");
		
				                               
	
    } //end of the main method
    
} //end of the class

Output

The entered character k is an Alphabet

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

5 comments on “Java program to check a character is an alphabet or not”


  • Shekhar Suman

    Scanner sc=new Scanner(System.in);
    String str=sc.nextLine();
    char ch=str.charAt(0);
    if (str.length()==1){
    if(ch>=65 && ch=96 && ch<=122){
    System.out.println("Alphabet");
    }else{
    System.out.println("Not alphabet");
    }
    }else{
    System.out.println("Not alphabet");
    }


  • Palak Gupta

    //Check whether a character is a alphabet or not
    public static void ischaracter(char ch) {
    char ch2= Character.toUpperCase(ch);
    int t=ch2;

    if(t>65 && t<90){
    System.out.println("The entire character is alphabet");
    }
    else{
    System.out.println("the entire charachter is not alphabet");
    }

    }


  • Siddhesh

    import java.util.Scanner;

    public class Alphabet {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    for (int i = 1; i = 65 && ch = 97 && ch <= 122) {
    System.out.println(ch + " is a alphabet");
    } else {
    System.out.println(ch + " is not not alphabet");
    }
    }
    }
    }


  • Shilpa

    import java.util.Scanner;

    public class AlphabetOrNot {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner s = new Scanner(System.in);
    char c1 = s.next().charAt(0);
    char c = java. lang. Character. toLowerCase(c1);

    if(c == ‘a’ || c == ‘b’ || c ==’c’ || c==’d’ || c==’e’ ||
    c == ‘f’ || c == ‘g’ || c ==’h’ || c==’i’ || c==’j’ ||
    c == ‘k’ || c == ‘l’ || c ==’m’ || c==’n’ || c==’o’ ||
    c == ‘p’ || c == ‘q’ || c ==’r’ || c==’s’ || c==’t’ ||
    c == ‘u’ || c == ‘v’ || c ==’w’ || c==’x’ || c==’y’ || c ==’z’) {
    System.out.println(“Alphabet”);
    }else {
    System.out.println(“Not Alphabet”);
    }
    }

    }


  • 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");
    }

    }