Java Program to Check Whether a Character is a Vowel or Consonant
The English alphabet has five vowels: A, E, I, O and U (in uppercase or lowercase). Every other letter is a consonant. Digits and symbols are neither.
This page shows four ways to check whether a character is a vowel or a consonant in Java.
1. Vowel or Consonant Using if-else
The character is first converted to lowercase with Character.toLowerCase(), so only five comparisons are needed instead of ten.
public class VowelConsonant {
public static void main(String[] args) {
char[] letters = {'K', 'e', 'U', 'z'};
for (char ch : letters) {
char c = Character.toLowerCase(ch);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
}
}
}
K is a consonant. e is a vowel. U is a vowel. z is a consonant.
2. Vowel or Consonant with User Input
When the character comes from the user, check that it is a letter first. Otherwise a digit or symbol would be reported as a consonant.
import java.util.Scanner;
public class VowelConsonantInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
scanner.close();
char c = Character.toLowerCase(ch);
if (c < 'a' || c > 'z') {
System.out.println(ch + " is not a letter.");
} else if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
}
}
Enter a character: e e is a vowel.
Enter a character: R R is a consonant.
Enter a character: 9 9 is not a letter.
3. Vowel or Consonant Using switch
A switch statement can list several labels for the same case. Since Java 14, you can write them in one line separated by commas with the arrow (->) syntax.
public class VowelConsonantSwitch {
public static void main(String[] args) {
char[] letters = {'K', 'e', 'O', 'b'};
for (char ch : letters) {
switch (Character.toLowerCase(ch)) {
case 'a', 'e', 'i', 'o', 'u' -> System.out.println(ch + " is a vowel.");
default -> System.out.println(ch + " is a consonant.");
}
}
}
}
K is a consonant. e is a vowel. O is a vowel. b is a consonant.
4. Vowel or Consonant Using a Method
The check can be moved into a reusable method. "aeiouAEIOU".indexOf(ch) returns the position of the character in the string, or -1 if it is not there.
public class VowelConsonantMethod {
static boolean isVowel(char ch) {
return "aeiouAEIOU".indexOf(ch) != -1;
}
public static void main(String[] args) {
String word = "Java";
for (char ch : word.toCharArray()) {
System.out.println(ch + " is a " + (isVowel(ch) ? "vowel" : "consonant") + ".");
}
}
}
J is a consonant. a is a vowel. v is a consonant. a is a vowel.
5. Summary
- A, E, I, O and U are vowels; every other letter is a consonant.
- Convert the character to lowercase first to halve the number of comparisons.
- Check that the input is a letter, otherwise digits and symbols look like consonants.
- A
switchwith several labels, or"aeiouAEIOU".indexOf(ch), keeps the check short.