English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)/)

Java Reader/Writer

Java other topics

Java program to check if a letter is a vowel or consonant

Comprehensive Java Examples

In this program, you will learn how to use if..else and switch statements in Java to check if a letter is a vowel or consonant.

Example1Use if..else statements to check if the letter is a vowel or consonant

public class VowelConsonant {
    public static void main(String[] args) {
        char ch = 'i';
        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 ");
    }
}

When running the program, the output is:

i is a vowel

In the above program, 'i' is stored in the char variable ch. In Java, double quotes (" ") are used for strings, and single quotes (' ') are used for characters.

Now, to check if ch is a vowel, check if ch is any of the following ('a', 'e', 'i', 'o', 'u'). You can complete it with a simple if..else statement.

We can also use the switch statement in Java to check for vowels or consonants.

Example2: Check if the letter is a vowel or consonant using a switch statement

public class VowelConsonant {
    public static void main(String[] args) {
        char ch = 'z';
        switch (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 ");
        }
    }
}

When running the program, the output is:

z is a consonant

In the above program, we did not use a long if condition statement, but replaced it with a switch case statement.

If ch is one of the following ('a', 'e', 'i', 'o', 'u'), then output the vowel. Otherwise, execute default and print the consonant on the screen.

Comprehensive Java Examples