In Java, we can check if a character is an alphabet using either an if-else condition or a ternary operator. This article focuses on explaining this concept through examples, an algorithm, detailed logic, and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to develop a Java program to check whether a given input character is an alphabet or not. The program should take a character as input and output whether it is an alphabet or not.This above action should be done in our program. Let’s look at an example,
Example 1:
Input: A Output: It is Alphabet
Here for the input A, Output is “it is Alphabet” , as A is an alphabet.
Example 2:
Input: 3 Output: It is not an Alphabet
Here for the input 3, Output is “it is not an Alphabet”, as 3 is a digit.
Algorithm to Check Character is Alphabet or not
- Start : Begin the algorithm with input character ch
- Characters take only one character at one time
- We use relational operators (>= and <=).
- Condition check: We will take alphabets from A to Z and a to z
- When the character is greater than or equal to ‘A’ or ‘a’ and less than or equal to ‘Z’ or ‘z’ then we say, the given input is alphabet
- Else, we say the given input is not an alphabet.
- End: The algorithm terminates with the result of the condition check.
Logic to Check Character is Alphabet or Not
There are multiple logics to check a given input is alphabet or not in Java Programing language.
Logic 1: Using if-else
- Take an input value (char type) from user
- In Scanner class, we don’t have a method to read char type,so use
- char ch=scanner.next().charAt(0)
- This will gives us character value at index 0. As char can store one value at a time, we can read values from user.
- char ch=scanner.next().charAt(0)
- Read and Store it
- If character greater than or equal to ‘A’ or ‘a’ and less than or equal to ‘Z’ or ‘z’, then we say it is alphabet otherwise not.
if ((ch>= 'A' && ch<='Z') || (ch>='a' && ch<='z'))
System.out.println("Given input character is Alphabet");
else
System.out.println(ch+"is not an alphabet");
Logic 2: Ternary Operator
- This operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
- When character greater than or equal to ‘A’ or ‘a’ and less than or equal to ‘Z’ or ‘z’, then we say it is alphabet otherwise not.
result =((ch>= 'A' && ch<='Z') || (ch>='a' && ch<='z'))?
ch+"is alphabet": ch+" is not alphabet";
Java Program to check character is Alphabet or not
Program 1: Using if-else
In the below program we are using the if else statement to check character is Alphabet or not in Java.
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter a character");
char ch = s.next().charAt(0);
if((ch>='A' && ch<='Z') ||(ch>='a'&& ch<='z'))
System.out.println("Given character is alphabet");
else
System.out.println("given char is not a Alphabet");
s.close();
}
}
Output:
Enter a character
M
Given character is alphabet
Program Explanation to check character is Alphabet or not
- The program imports the Scanner class to facilitate user input.
- Defines a class named Alphabet.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a character.
- Reads the input character using the next() method and retrieves the first character using charAt(0).
- Checks if the character is within the range of uppercase or lowercase alphabets using ASCII values.
- If the character is within this range, it prints “Given character is alphabet”.
- If the character is not within this range, it prints “Given character is not an alphabet”.
- Closes the Scanner object to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check character is Alphabet or not in Java.
import java.util.Scanner;
public class Alphabet {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
System.out.println("Enter a character");
char ch = s.next().charAt(0);
String result;
result =((ch>='A' && ch<='Z') ||(ch>='a' && ch<='z'))?
ch+ " is alphabet": ch+ " is not an alphabet";
System.out.println(result);
s.close();
}
}
Output:
Enter a character
r
r is alphabet
Program Explanation to check character is Alphabet or not
- The program imports the
Scanner
class to read input from the user. - It defines a class named
Alphabet
with amain
method. - A
Scanner
objects
is created to take input from the user. - The user is asked to enter a character.
- The character is read using
s.next().charAt(0)
and stored in variablech
. - A ternary operator checks if
ch
is an uppercase or lowercase letter. - If true,
result
is assigned the valuech + " is an alphabet"
. - Otherwise,
result
is assigned the valuech + " is not an alphabet"
. - The result is printed to the console.
- The
Scanner
object is closed to free up resources.
Conclusion
In this article, we explored how to check if a character is alphabet or not using if-else statement and the ternary operator. We understood the logic to do this and moved on to writing the program.
For more information on related topics, you might find these articles helpful:
- Understanding the Conditional statements in Java
- Concepts of Operators in Java
- Top 10 Java Programming Tips
We hope this tutorial has been helpful. If you have any doubts or need any further clarification, feel free to leave a comment below. Don’t forget to subscribe our newsletter to get more updates on Java.
Happy Programming!