Java program to check whether input character is vowel or consonant using if else
In this article, you will learn how to make a Java program to check whether the input character is vowel or consonant using if-else in the Java programming language.
Examples
Enter a character to check: E
'E' is a vowel character.
Enter a character to check: F
'F' is a consonant character.
You should have knowledge of the following topics in Java programming to understand this program:
- Java
java.util.Scanner
package - Java
if-else
condition statement - Java
System.out.println()
function
Vowel Characters
Consonant Characters
Source Code
// Java program to check whether input character is vowel or consonant using if-else
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character to check: ");
char random_character = in.next().charAt(0);
boolean lowercase = false, uppercase = false;
// Character.isalpha() function will check
// whether character is alphabetic or non-alphabetic
if (Character.isAlphabetic(random_character)) {
// it will be return 1
// if input character matched with these small characters
lowercase = random_character == 'a' || random_character == 'e' || random_character == 'i' || random_character == 'o' || random_character == 'u';
// it will be return 1
// if input character matched with these capital characters
uppercase = random_character == 'A' || random_character == 'E' || random_character == 'I' || random_character == 'O' || random_character == 'U';
if (lowercase || uppercase)
System.out.println("\n'" + random_character + "' is a vowel character.");
else
System.out.println("\n'" + random_character + "' is a consonant character.");
} else {
System.out.println("\nSorry, You entered Non-Alphabetic character!");
}
}
}
Output
Enter a character to check: E
'E' is a vowel character.
Explanation
In this program, we have taken input E
from the user and checked for its alphabetic or non-alphabetic character.
Then we matched this character with a series of vowel characters, If matched with the vowel characters series then it's vowel character.
If didn't match with the vowel characters series then It's a consonant character.