Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Program to Check Whether a Character is Alphabet or Not
In this article we will see how we can check whether a character is alphabet or not. So before going into the actual topic, let’s know about alphabet first.
Alphabet:
In English language there are 26 letters which are called English alphabets.
- Capital Letters: A to Z
- Small Letters: a to z
There are several ways in which we can check and see if an entered character is an alphabet or not.
Method-1 : Using if-else
To check whether the character is an alphabet or not we can use if-else statement.
Approach:
- Take one character input from user.
- Check if the character falls under the alphabets’ range.
- If it falls under the alphabet’s range mentioned in if condition then it is an alphabet.
- Otherwise it is not an alphabet.
Program:
import java.util.Scanner; class checkAlphabet { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter a character"); //Taking the character as input from the user char ch = scan.next().charAt(0); //CHecking wheteher the character comes under alphabets if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')) { System.out.print(ch+" is an alphabet "); } else { System.out.print(ch+" is not an Alphabet "); } } }
Output: //CASE-1 Enter a character f f is an alphabet // CASE-2 Enter a character 3 3 is not an Alphabet
Method-2 : Using ASCII
To check whether the character is an alphabet or not we can use ASCII values.
Approach:
- Like the previous example we can do the same thing however rather than using the alphabetical range, we will be using the ASCII values. ASCII value is the integer value allotted to each character.
Program:
import java.util.Scanner; class checkAlphabet{ public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter a character"); //Taking the character as input from the user char ch = scan.next().charAt(0); //Checking wheteher the character comes under alphabets using ASCII if((ch>=97 && ch<=122)||(ch>=65 && ch<=90)) { System.out.print(ch+" is an alphabet "); } else { System.out.print(ch+" is not an Alphabet "); } } }
Output: //CASE-1 Enter a character f f is an alphabet // CASE-2 Enter a character 3 3 is not an Alphabet
Method-3 : Using isAlphabetic() method
To check whether the character is an alphabet or not we can use in-built function isAlphabetic( )
.
Approach:
- Take the character user input.
- Pass it to i
sAlphabetic()
method. - This method will check it is an alphabet or not.
Program:
import java.util.Scanner; class checkAlphabet { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.println("Enter a character"); //Taking the character as input from the user char ch = scan.next().charAt(0); //CHecking wheteher the character comes under alphabets using library function if(Character.isAlphabetic(ch)) { System.out.print(ch+" is an alphabet "); } else { System.out.print(ch+" is not an Alphabet "); } } }
Output: //CASE-1 Enter a character f f is an alphabet // CASE-2 Enter a character 3 3 is not an Alphabet
Get started with learning the programming language Java from beginner to experienced level by referring to our collection of Java Programs with Source Code and become a pro in the subject.
Related Java Decision Making and Loop Program:
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Check Armstrong Number
- Java Program to Display Armstrong Number Between Two Intervals
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)