0
0
JavaProgramBeginner · 2 min read

Java Program to Check Vowel or Consonant

In Java, you can check if a character is a vowel or consonant by using if statements to compare the character against vowels like 'a', 'e', 'i', 'o', 'u' (case-insensitive), and print "Vowel" if it matches or "Consonant" otherwise.
📋

Examples

Inputa
Outputa is a vowel
InputB
OutputB is a consonant
Input1
Output1 is not an alphabet
🧠

How to Think About It

To check if a character is a vowel or consonant, first verify if it is a letter. If it is not a letter, say it is not an alphabet. If it is a letter, convert it to lowercase to simplify checks. Then compare it to the vowels 'a', 'e', 'i', 'o', and 'u'. If it matches any of these, it is a vowel; otherwise, it is a consonant.
📐

Algorithm

1
Get a character input from the user
2
Check if the character is a letter (a-z or A-Z)
3
If not a letter, print it is not an alphabet and stop
4
Convert the character to lowercase
5
Check if the character is 'a', 'e', 'i', 'o', or 'u'
6
If yes, print it is a vowel; otherwise, print it is a consonant
💻

Code

java
import java.util.Scanner;

public class VowelOrConsonant {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        if (!Character.isLetter(ch)) {
            System.out.println(ch + " is not an alphabet");
        } else {
            char lowerCh = Character.toLowerCase(ch);
            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");
            }
        }
        scanner.close();
    }
}
Output
Enter a character: a a is a vowel
🔍

Dry Run

Let's trace the input 'B' through the code

1

Input character

User inputs 'B'

2

Check if letter

'B' is a letter, so continue

3

Convert to lowercase

'B' becomes 'b'

4

Check vowel

'b' is not 'a', 'e', 'i', 'o', or 'u'

5

Print result

Print 'B is a consonant'

StepCharacterIs Letter?LowercaseIs Vowel?Output
1BYesbNoB is a consonant
💡

Why This Works

Step 1: Check if input is a letter

We use Character.isLetter() to ensure the input is an alphabet letter before checking vowels or consonants.

Step 2: Convert to lowercase

Converting to lowercase with Character.toLowerCase() simplifies vowel checking by handling uppercase and lowercase uniformly.

Step 3: Compare with vowels

We compare the character with vowels using if conditions; if it matches, we print vowel, else consonant.

🔄

Alternative Approaches

Using switch-case
java
import java.util.Scanner;

public class VowelOrConsonantSwitch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        if (!Character.isLetter(ch)) {
            System.out.println(ch + " is not an alphabet");
        } else {
            switch (Character.toLowerCase(ch)) {
                case 'a': case 'e': case 'i': case 'o': case 'u':
                    System.out.println(ch + " is a vowel");
                    break;
                default:
                    System.out.println(ch + " is a consonant");
            }
        }
        scanner.close();
    }
}
Switch-case can be cleaner for multiple fixed comparisons but is similar in performance.
Using String contains method
java
import java.util.Scanner;

public class VowelOrConsonantString {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        if (!Character.isLetter(ch)) {
            System.out.println(ch + " is not an alphabet");
        } else {
            String vowels = "aeiou";
            if (vowels.indexOf(Character.toLowerCase(ch)) != -1) {
                System.out.println(ch + " is a vowel");
            } else {
                System.out.println(ch + " is a consonant");
            }
        }
        scanner.close();
    }
}
Using a string to check vowels is concise and easy to extend but slightly less explicit.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of checks regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used, so space complexity is constant O(1).

Which Approach is Fastest?

All approaches (if-else, switch-case, string contains) run in constant time; differences are minimal and mostly readability-based.

ApproachTimeSpaceBest For
If-elseO(1)O(1)Simple and clear for beginners
Switch-caseO(1)O(1)Cleaner syntax for multiple fixed cases
String containsO(1)O(1)Concise and easy to extend vowel list
💡
Always check if the input is a letter before checking vowels or consonants to avoid errors.
⚠️
Beginners often forget to handle uppercase letters, causing wrong results for capital vowels.