In Java, we can determine if a character is a vowel, consonant, or special character using either if-else conditions or the ternary operator. This article focuses on learning through examples, algorithms, detailed logic, and program explanations for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to write a Java program to determine if a given character is a vowel, consonant, or special character. The program will take a character as input and output whether it is a vowel, consonant, or special character. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: M
Output: M is Consonant
Here for the input M, Output is ” M is Consonant” as M is not an vowel
Example 2:
Input: $ Output: It is a special character
Here, for the input $, Output is “It is a special character” as it is a symbol
Algorithm to check character is Vowel or Consonant or Special Character
- Start: Begin the program with character input
ch
. - Reading Input: Use the
next()
method to read and extract the first character usingcharAt(0)
. - Condition Check:
- If the character is ‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (uppercase or lowercase), then it is categorized as a vowel.
- Otherwise, check if the character falls within the range ‘A’ to ‘Z’ (for uppercase) or ‘a’ to ‘z’ (for lowercase). If so, categorize it as a consonant.
- ch>=’A’ && ch<‘Z’ or ch>=’a’ && ch<=’z’
- Next, check if the character is between ‘0’ and ‘9’. If true, classify it as a digit.
- ch>=’0 && ch<=’9′
- If none of the above conditions are met, classify the character as a special character.
Logic to check character is Vowel or Consonant or special character
There are multiple logics to check a given input character is vowel or consonant or special character in Java Programing language.
Logic 1: Using if-else
- If the character falls within ‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, it is considered a vowel. Otherwise, using an else-if statement, the program checks the next condition immediately, which is if the character falls within ‘A’ to ‘Z’ or ‘a’ to ‘z’, then it is considered a consonant.
- If the character is neither a vowel nor a consonant, the program checks if it is a digit (0 to 9). If it is a digit, the program prints the digit; otherwise, it prints that the character is a special character.
if((ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')||
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')) {
System.out.println(ch+" is vowel");
}
else if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){
System.out.println(ch+" is consonant");
}
else if(ch>='0'&& ch<='9') {
System.out.println(ch+" is a digit");
}
else {
System.out.println(ch+" special Character");
}
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.
- If the character falls within ‘A’, ‘E’, ‘I’, ‘O’, ‘U’, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, it is considered a vowel.Otherwise, the program checks the next condition immediately, which is if the character falls within ‘A’ to ‘Z’ or ‘a’ to ‘z’, then it is considered a consonant.
- If the character is neither a vowel nor a consonant, the program checks if it is a digit (0 to 9). If it is a digit, the program prints the digit; otherwise, it prints that the character is a special character and assign the output to the result variable.
String result;
result=((ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')||
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')) ?
ch+ " is vowel":
((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))?
ch+" is consonant":
(ch>='0'&& ch<='9') ?
ch+" is a digit":
ch+"special Character";
System.out.println(result);
Java Program to Check Character is Vowel or Consonant or Special Character
Program 1: Using if-else
In the below program we are using if-else to check Character is vowel or consonant or special character in Java.
import java.util.Scanner;
public class VoewlConsonantSpecialChar {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the character");
char ch = s.next().charAt(0);
if((ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')||(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')) {
System.out.println(ch+" is vowel");
}
else if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){
System.out.println(ch+" is consonant");
}
else if(ch>='0'&& ch<='9') {
System.out.println(ch+" is a digit");
}
else {
System.out.println(ch+" special Character");
}
s.close();
}
}
Output:
Enter the character
5
5 is a digit
Java program explanation to check given character is Vowel or Consonant or Special character
- The program imports the
Scanner
class from thejava.util
package to facilitate user input. - Program defines a class named
VoewlConsonantSpecialChar
. - Program contains the main method where the program execution starts.
- Asks the user to enter a character.
- Reads the user input character using the
Scanner
object’snext()
method and extracts the first character usingcharAt(0)
. - It checks if the input character is a vowel (either uppercase or lowercase).
- If the character is ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (uppercase) or ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (lowercase), it prints that the character is a vowel.
- Otherwise, if the character is an alphabet (either uppercase or lowercase) but not a vowel, it prints that the character is a consonant.
- If the character is a digit (0 to 9), it prints that the character is a digit.
- If the character doesn’t fall into any of the above categories, it prints that the character is a special character.
- It closes the
Scanner
objects
to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to check Character is vowel or consonant or special character in Java.
import java.util.Scanner;
public class VoewlConsonantSpecialChar {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the character");
char ch = s.next().charAt(0);
String result;
result=((ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')||
(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')) ?
ch+ " is vowel":
((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'))?
ch+" is consonant":
(ch>='0'&& ch<='9') ?
ch+" is a digit":
ch+"special Character";
System.out.println(result);
s.close();
}
}
Output:
Enter the character
$
$special Character
Java program explanation to check given character is Vowel or Consonant or Special character
- The program imports the
Scanner
class from thejava.util
package to facilitate user input. - Program defines a class named
VoewlConsonantSpecialChar
. - It contains the main method where the program execution starts.
- Asks the user to enter a character.
- Reads the user input character using the
Scanner
object’snext()
method and extracts the first character usingcharAt(0)
. - It uses nested ternary operators to determine whether the input character is a vowel, consonant, digit, or special character.
- If the character is ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ (uppercase) or ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ (lowercase), it assigns a message indicating that the character is a vowel.
- Otherwise, if the character is an alphabet (either uppercase or lowercase) but not a vowel, it assigns a message indicating that the character is a consonant.
- Else, if the character is a digit (0 to 9), it assigns a message indicating that the character is a digit.
- If the character doesn’t fall into any of the above categories, it assigns a message indicating that the character is a special character.
- It prints the message assigned based on the ternary operator evaluation.
- It closes the
Scanner
objects
to release system resources.
Conclusion
In this article, we explored how to check if a character is vowel or consonant or special character using if-else and the ternary operator. We understood the logic and proceed to implement the program.
For more information on related topics, you might find these articles helpful:
- Understanding the concepts of Conditional statements in Java
- Understanding the Operators in Java
- Top 10 Java Programming tips
We hope you find this article helpful. If you have any questions or need clarification, please leave a comment below. Don’t forget to subscribe to our newsletter for more updates on Java programming tips and tutorials.
Happy Programming!