In Java, to check if a character is Digit or Alphabet in java we can use if-else-if condition or Ternary Operator. In this article, we are focusing on learning through examples, algorithm, logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to write a java program to check whether given input is Digit or Alphabet. The Program should take an character as input and It should print the result whether it is digit or alphabet. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: B
Output: it is Alphabet
Example 2:
Input: 6 Output: it is a digit
Algorithm to Check Input is Digit or Alphabet
- Start : Begin the algorithm with input character ch
- Characters take only one character at one time
- use >= and <= operators .
- Condition check: Here we need to check two conditions, one to check alphabets and other to check digits. 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.
- ch>=’A’ && ch>==’Z’ or ch>=’a’ && ch<=’z’ , otherwise go to check condition for digits.
- We will take digits from Zero to Nine
- When the character is greater than or equal to zero, and less than or equal to nine then we say, the given input is digit
- ch>=’0′ && ch<=’9′
- Else, we say the given input is not digit or it is symbol.
- 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.
- End: The algorithm terminates with the result of the condition check.
Logic to Check input is Digit or Alphabet
There are multiple logics to check a given input is digit or alphabet in Java Programing language.
Logic 1: Using if-else-if
- If the input character falls within ‘A’ to ‘Z’ or ‘a’ to ‘z’, then it is considered an alphabet. Otherwise, using else-if, it will check the next condition: if the character falls within the range ‘0’ to ‘9’, then it is a digit. Otherwise, it is a symbol.
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
System.out.println("Given input character is alphabet");
else if (ch>= '0' && ch<='9')
System.out.println("Given input character is digit");
else
System.out.println(ch+"is a symbol");
Logic 2: Ternary Operator
- This operator takes three operands and works similarly to if-else statements. If the condition is true, it evaluates the first expression, if the condition is false, it evaluates the second expression.
- If the input character falls within ‘A’ to ‘Z’ or ‘a’ to ‘z’, it is considered an alphabet. Otherwise, it checks the next condition: if the character falls within the range ‘0’ to ‘9’, it is a digit. If neither condition is met, the character is a symbol. The output is then assigned to the result variable.
result=((ch>='A'&&ch<='Z')||(ch>='a' &&ch<='z'))?
ch+"is alphabet" :(ch>='0' && ch<='9')?
ch+" is digit":ch+" is symbol";
System.out.println(result);
Java Program to Check input is digit or Alphabet
Program 1: Using if-else-if
In the below program we are using if -else-if to check input is digit or alphabet in Java.
import java.util.Scanner;
public class AlphaDigit {
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(ch+ " is alphabet");
}
else if(ch>='0' && ch<='9') {
System.out.println(ch + " is a digit");
}
else {
System.out.println(ch +" is Symbol");
}
s.close();
}
}
Output:
Enter a character
6
6 is a digit
Enter a character
$
$ is Symbol
Enter a character
R
R is alphabet
Program Explanation to check input is a digit or Alphabet
- The java program imports the Scanner class to facilitate user input.
- Defines a class named AlphaDigit.
- 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 an alphabet (uppercase or lowercase) using ASCII values.
- If the character is an alphabet, it prints that it’s an alphabet.
- Otherwise, the character is a digit (0-9), it prints that it’s a digit.
- Else, the character doesn’t fit into the above categories, it prints that it’s a symbol.
- Closes the Scanner object to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check input is digit or alphabet in Java.
import java.util.Scanner;
public class AlphaDigit {
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>='0' && ch<='9')? ch + " is a digit":
ch +" is Symbol";
System.out.println(result);
s.close();
}
}
Output:
Enter a character
*
* is Symbol
Enter a character
9
9 is a digit
Enter a character
L
L is alphabet
Program Explanation to check input is a digit or Alphabet
- The program imports the Scanner class to facilitate user input.
- Defines a class named AlphaDigit.
- 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).
- Uses a ternary operator to assign the appropriate message to the variable ‘result’ based on whether the character is an alphabet, digit, or symbol.
- Prints the ‘result’ message.
- Closes the Scanner object to release system resources.
Conclusion
In this post, we explored how to check if the input character is digit or alphabet using if-else-if statement and the ternary operator. We have learned the logic and also learnt how to write the program.
For more information on related topics, you might check these articles:-
- Understanding the conditional statements in java
- Understanding the operators in Java
- Java program to check input character is digit or not
- Program to check input character is alphabet or not in Java
We hope you find this article helpful. If you have any doubts or need any further clarification, feel free to leave a comment below. Don’t forget to subscribe to our newsletter for more Java Programming tips and tutorials.
Happy Programming!