In this tutorial, we will learn how to write a Java program to check whether a character is a vowel or a consonant. In the English alphabet, there are five vowels: A, E, I, O, and U. The remaining 21 letters are consonants.
For example:
- A is a vowel.
- e is a vowel.
- B is a consonant.
- z is a consonant.
Let’s explore different ways to write a Java program to check whether a character is a vowel or a consonant.
Java Program to Check Vowel or Consonant Without User Input
Let us write a Java program to check whether a character is a vowel or a consonant. In this approach, we will create a variable named ch of type char and assign a character value directly to it.
This approach is very useful for beginners who want to focus entirely on core conditional logic without complicating the code with the Scanner class or user input handling.
Java Program Code
public class VowelConsonantWithoutInput {
public static void main(String[] args) {
// Directly assigning a character to the variable
char ch = 'e';
// Convert the character to lowercase to handle both upper and lower cases
char lowerCh = Character.toLowerCase(ch);
// Check if the character is a vowel
if (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
}
}Expected Output:
e is a vowel.
In this example program:
- Variable Initialization:
char ch = 'e';declares a character variable named ch and assigns a value directly to it. - Case Conversion:
Character.toLowerCase(ch);converts the character to its lowercase equivalent. This makes it much easier because we only need to check for small vowels (a, e, i, o, u) instead of writing separate rules for capital letters like A or E. - Checking for a Vowel: The if statement checks if the letter matches any of the five vowels. If it does, the program prints a message saying it is a vowel.
- Handling Consonants: If the letter doesn’t match any of the vowels, the program moves to the else block and prints that it is a consonant.
Java Program to Check Vowel or Consonant Using Scanner Class
In the previous program, we have assigned a fixed character value to a variable directly and checked whether it is vowel or a consonant. Now we will learn how to write a Java program to check whether a character is a vowel or a consonant by taking input directly from the user.
Instead of assigning a fixed character in the code, we will use Java’s Scanner class to read a letter entered through the keyboard at runtime.
Java Program Code
import java.util.Scanner;
public class VowelConsonantScanner {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a character
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
// Close the scanner to prevent memory leaks
scanner.close();
// Convert the character to lowercase to handle both upper and lower cases
char lowerCh = Character.toLowerCase(ch);
// Check if the input is a valid alphabet letter
if (!Character.isLetter(ch)) {
System.out.println(ch + " is not a valid letter.");
} else if (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
}
}Expected Output:
Enter a character: E E is a vowel.
In this example program:
- Taking user input:
import java.util.Scanner;import the Scanner class to read input from the user’s keyboard. - Reading a character: The statement
scanner.next().charAt(0);reads the first character entered by the user and stores it in the variable ch. - Input validation:
Character.isLetter(ch);checks if the user actually typed a letter instead of a number or symbol, making the program more reliable. - Handling upper and lower case: Character.toLowerCase(ch); turns the entered letter into lowercase so the program can easily check against small vowels (a, e, i, o, u).
- Checking vowels and consonants: The if-else statement checks if the alphabet letter matches a vowel. If it does, it prints that it is a vowel; otherwise, it prints that it is a consonant.
- Close the Scanner: The
sc.close();statement closes the input scanner.
Java Program to Check Whether a Character Is a Vowel or Consonant Using Switch
We will learn how to write a Java program to check whether a character is a vowel or a consonant using the modern switch expression syntax introduced in Java 14. This approach makes the code much cleaner, more concise, and easier to read compared to traditional if-else chains or old switch structures.
Java Program Code
import java.util.Scanner;
public class VowelConsonantModernSwitch {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a character
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
// Close the scanner to free up system resources
scanner.close();
// Validate if the input is actually an alphabetic letter
if (!Character.isLetter(ch)) {
System.out.println(ch + " is not a valid letter.");
return; // Exit the program if input is invalid
}
// Convert the character to lowercase to handle both upper and lower cases uniformly
char lowerCh = Character.toLowerCase(ch);
// Use the modern switch expression (Java 14+) to determine vowel or consonant
String result = switch (lowerCh) {
case 'a', 'e', 'i', 'o', 'u' -> " is a vowel.";
default -> " is a consonant.";
};
// Print the final output combining the entered character and the result
System.out.println(ch + result);
}
}Expected Output:
Enter a character: A A is a vowel.
In this example program:
- Multiple case labels: case ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ combines all five vowels into a single line using commas, which avoids writing repetitive code for each vowel.
- Arrow operator: The
->syntax replaces the old colon and eliminates the need for break statements, preventing accidental fall-through errors. - Switch expression: The switch block directly returns a string result based on the character matched, making the logic compact.
- Output display:
System.out.println(ch + result);combines the user input character with the result string to print the final message.
Java Program to Check Whether a Character Is a Vowel or Consonant Using Ternary Operator
Let’s learn how to write a Java program to check whether a character is a vowel or a consonant using the ternary operator. The ternary operator acts as a shorthand way of writing an if-else statement, allowing us to evaluate conditions and assign values in a single, concise line.
Java Program Code
import java.util.Scanner;
public class VowelConsonantTernary {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a character
System.out.print("Enter a character: ");
char ch = scanner.next().charAt(0);
// Close the scanner to free up system resources
scanner.close();
// Validate if the input is an alphabetic letter
if (!Character.isLetter(ch)) {
System.out.println(ch + " is not a valid letter.");
return;
}
// Convert the character to lowercase for uniform checking
char lowerCh = Character.toLowerCase(ch);
// Use the ternary operator to check if it is a vowel or consonant
String result = (lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u')
? " is a vowel."
: " is a consonant.";
// Print the final output combining the entered character and the result
System.out.println(ch + result);
}
}Expected Output:
Enter a character: O O is a vowel.
In this example:
- Ternary syntax: The ternary operator uses the format
condition ? true_expression : false_expressionto perform inline decision-making without writing a full multi-line if-else block. - Condition check:
(lowerCh == 'a' || lowerCh == 'e' || lowerCh == 'i' || lowerCh == 'o' || lowerCh == 'u')checks whether the entered letter matches any of the five standard vowels. - True and false parts: If the condition is true, the message ” is a vowel.” is assigned to the result variable. If it is false, ” is a consonant.” is assigned instead.
- Displaying the message:
System.out.println(ch + result);prints out the final combined result for the user.





