Java ProgramsControl FlowCheck Character Is Vowel or Consonant

Check Character Is Vowel or Consonant in Java

beginner·  Control Flow  ·  Conditional Statements

Problem

A vowel is one of the letters a, e, i, o, u (in either case) — every other letter is a consonant.

Given a single character, determine whether it is a vowel or a consonant.

Input
'e'
Output
'e' is a vowel

Java Program

Java
public class VowelConsonantCheck { public static void main(String[] args) { char ch = 'e'; boolean isVowel = ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || // lowercase vowels ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'; // uppercase vowels System.out.println("'" + ch + "' is a " + (isVowel ? "vowel" : "consonant")); } }

Output

'e' is a vowel

Core Logic

Comparing the character directly against each of the five vowels, in both upper and lower case, settles the question with a single chained condition.

How It Works
  1. 1ch is compared against 'a', 'e', 'i', 'o', 'u' and their uppercase equivalents using ||.
  2. 2If any comparison matches, the character is a vowel; otherwise it's treated as a consonant.
  3. 3The check assumes ch is already a letter — no digit or space handling is layered in.
For ch = 'e', the comparison against 'e' matches immediately, so the character is reported as a vowel.
💡

Key Point: Chaining all ten comparisons (5 vowels × 2 cases) with || avoids needing to convert the character's case first.

Key Concepts

character comparisonlogical ORcase handling

Approach 2: Java 8

Java
public class VowelConsonantCheckStream { public static void main(String[] args) { char ch = 'e'; // Checks whether ch appears anywhere in the vowel string, either case boolean isVowel = "aeiouAEIOU".chars().anyMatch(v -> v == ch); System.out.println("'" + ch + "' is a " + (isVowel ? "vowel" : "consonant")); } }

Output

'e' is a vowel

Core Logic

A short literal string of every vowel, in both cases, can be searched with a stream instead of writing out ten comparisons by hand.

How It Works
  1. 1"aeiouAEIOU".chars() turns the ten-character vowel string into an IntStream of character codes.
  2. 2.anyMatch(v -> v == ch) checks whether the given character's code appears anywhere in that stream.
  3. 3The boolean result feeds directly into the vowel/consonant message.
For ch = 'e', the stream finds a match at the second character of "aeiouAEIOU", so anyMatch() returns true.
💡

Key Point: This reads as 'is ch one of these characters', arguably clearer than a ten-way || chain, at the cost of stream overhead for what's otherwise a trivial check.

Key Concepts

Streamchars()anyMatch()

Related Programs