Java Program to Check Whether the Character is Vowel or Consonant
In this article, we are going to learn how to check whether a character is a vowel or a consonant. So, the task is, for any given character, we need to check if it is a vowel or a consonant. As we know, vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ and all the other characters (i.e. ‘b’, ‘c’, ‘d’, ‘f’ …..) are consonants.
Below is the image attached for better understanding.

Examples:
Input : char = 'r'
Output : Consonant
Input : char = 'e'
Output : Vowel
Different Approaches to Check Whether char is Vowel or Consonant
Approach 1: Using if else Statement
Here, in the below implementation we will use the if else statement to check if the stated character corresponds to any of the five vowels. And if it matches, “Vowel” is printed, else “Consonant” is printed.
Example 1:
// Java program to check whether input
// character is a vowel or consonant
// using if else statement
import java.io.*;
public class Geeks {
public static void main(String args[]) {
char ch = 'b';
// check using if else
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u') {
System.out.println(ch+" is a vowel");
}else {
System.out.println(ch+" is a consonant");
}
}
}
Output
b is a consonant
Example 2:
Alteration for capital letters.
// Java program to check whether input
// character is a vowel or consonant
import java.io.*;
public class Geeks {
public static void main(String args[]) {
char ch = 'B';
// check using if else
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u') {
System.out.println(ch+" is a vowel");
}else {
System.out.println(ch+" is a consonant");
}
}
}
Output
B is a consonant
Explanation: This is one of our approaches, in which we have checked for both small and capital characters whether is vowel or consonant
Approach 2: Using switch case
This approach will also check for vowel and consonant characters using switch case. Let us see the example below:
Example:
// Java program to check whether input
// character is a vowel or consonant
// using switch statement
import java.io.*;
public class Geeks {
public static void main(String args[]) {
char ch = 'i';
// check using switch case
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.print(ch+" is a vowel");
break;
default: System.out.print(ch+" is a consonant");
}
}
}
Output
i is a vowel
Approach 3: Using indexOf() Method
In this approach, we will use the indexOf() method to check if a given string has a particular character or not. If the character found in the string, then it will return that character, otherwise it will return -1.
Example 1:
// Java program to check whether input
// character is a vowel or consonant
// using indexOf() method
import java.io.*;
class Geeks {
public static void main(String args[]) {
char ch = 'c';
// Make the list of vowels
String str = "aeiouAEIOU";
// str is a list of vowels
if(str.indexOf(ch) == -1) {
System.out.print(ch+" is a consonant");
}else {
System.out.print(ch+" is a vowel");
}
}
}
Output
c is a consonant
Example 2: Using Scanner Class
We can also take the character as input from the user using the scanner class.
// Java program to check whether input
// character is a vowel or consonant
// using Scanner and switch statement
import java.util.Scanner;
public class Geeks {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a character: ");
// take only the first character
char ch = scanner.next().charAt(0);
// convert character to lowercase
ch = Character.toLowerCase(ch);
// check using switch case
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.print(ch + " is a vowel");
break;
default:
// check if it's a valid alphabet
if (Character.isLetter(ch))
System.out.print(ch + " is a consonant");
else
System.out.print("Invalid input. Please enter an alphabet.");
}
scanner.close();
}
}
Output:
