Table of Contents
Java program to check whether a character is an Alphabets or Not
In this article, we will discuss the concept of Java program to check whether a character is an Alphabets or Not
In this post, we are going to learn how to write a program to check whether given character is English alphabet or not using various ways in Java language
Code to check character is Alphabet or Not
Code to check the character is Alphabet or Not using If else
In this code, we are going to learn how to check the given character is English alphabet or not using if-else in Java language
Program 1
import java.util.Scanner; class Check_Eng_Alphabet1{//class declaration public static void main(String args[]){//main method char ch;//declare a character variable Scanner scan=new Scanner(System.in); //take input from the user for alphabets System.out.print("Enter a character for check Alphabets: "); //get input and store in ch variable ch=scan.next().charAt(0); //read the character if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){ //check the given character is Alphabet or not System.out.print(ch+" is an Alphabet: "); } else { System.out.print(ch+" is not an Alphabet: "); } } }
When the above code is executed, it produces the following result
Case 1
Enter a character for check Alphabets: r r is an Alphabets
Case 2
Enter a character for check Alphabets: 5 5 is not an Alphabets
Code to check the character is Alphabet or Not using Ascii
In this code, we are going to learn how to check the given character is English alphabet or not, using Ascii value in Java language
Program 2
import java.util.Scanner; class Check_Eng_Alphabet3{//class declaration public static void main(String args[]){ char ch;//declare character variable Scanner scan=new Scanner(System.in); //take input from the user System.out.print("Enter a character for check Alphabet: "); //get input and store in ch variable ch=scan.next().charAt(0); //read the input from the user if((ch>=97 && ch<=122)||(ch>=65 && ch<=90)){ System.out.print(ch+" is an Alphabet: "); }//using Ascii value to check Alphabets else { System.out.print(ch+" is not an Alphabet: "); } } }
When the above code is executed, it produces the following result
Case 1
Enter a character for check Alphabets A A is an Alphabets
Case 2
Enter a character for check Alphabets: # # is not an Alphabets
Code to check the character is Alphabet or Not using ternary operator
In this code, we are going to learn how to check the given character is English alphabet or not, using ternary operator in Java language
Program 3
import java.util.Scanner; class Check_Eng_Alphabet2{//class declaration public static void main(String args[]){//java main method char ch;//declare a character variable Scanner scan=new Scanner(System.in); //take input from the user System.out.print("Enter a character for check Alphabet: "); //get input and stord in ch variable ch=scan.next().charAt(0); //read the character String result=((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')) //check the given character is Alphabet or not using ternary ? ch + " is an Alphabet" : ch + " is not an Alphabet" ; System.out.print(result); } }
When the above code is executed, it produces the following result
Case 1
Enter a character for check Alphabets: Z Z is an Alphabets
Case 2
Enter a character for check Alphabets: * * is not an Alphabets
Code to check the character is Alphabet or Not using isAlphabetic()
In this code, we are going to learn how to check the given character is English alphabet or not, using isAlphabetic() method in Java language
Program 4
import java.util.Scanner; class Check_Eng_Alphabet4{//class declaration public static void main(String args[]){ char ch;//declare character variable Scanner scan=new Scanner(System.in); //take input from the user System.out.print("Please Enter a character for check Alphabet: "); //get input and store in ch variable ch=scan.next().charAt(0); //read the input from the user if(Character.isAlphabetic(ch)){ System.out.print(ch+" is an Alphabet: "); }//check the given character is Alphabet or not using isAlphabetic() function else { System.out.print(ch+" is not an Alphabet: "); } } }
When the above code is executed, it produces the following result
Case 1
Please Enter a character for check Alphabets: W W is an Alphabets
Case 2
Please Enter a character for check Alphabets: $ $ is not an Alphabets
Suggested post
Nested if statements in Java language
Do-while loop in Java language
Similar post
Java program to print all alphabets in given range
C program to print all alphabets in given range
C++ program to print all alphabets in given range
Java program to print all alphabets using loops
C program to print all alphabets using loops
C++ program to print all alphabets using loops
Java program to check whether a character is Alphabet or Not
C++ program to check whether a character is Alphabet or Not
C program to check whether a character is Alphabet or Not
Python program to check whether a character is Alphabet or Not