Check whether a character is a Vowel or Consonant

In this program, you will learn to check whether a character is a Vowel or Consonant in Java. The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and all remaining characters from the alphabet are consonants.

Let’s see a few java program examples of it below:

Example-1. Find if a character is a Vowel or Consonant in Java

Here, we check whether the given character matches any of the 5 vowels or not. If yes, the given character is a Vowel, else Consonant. Let’s assume that the user will always enter an alphabet character.

package com.javacodepoint.basics;

import java.util.Scanner;

public class CheckVowelConsonant {

	public static void main(String[] args) {
		// Create scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an alphabet character: ");
		char ch = sc.next().charAt(0);

		// Check Vowel or Consonant
		if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
			System.out.println("The given character is a Vowel.");
		} else {
			System.out.println("The given character is a Consonant.");
		}

	}

}

OUTPUT:

Enter an alphabet character:
a
The given character is a Vowel.

Example-2. handle capital letter Vowels

In this example, if user gives the character in capital letters also it will handle. Here we are checking given character with all 5 vowels in lowercase as well as uppercase.

package com.javacodepoint.basics;

import java.util.Scanner;

public class CheckVowelConsonant2 {

	public static void main(String[] args) {
		// Create scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an alphabet character: ");
		char ch = sc.next().charAt(0);

		// Check Vowel or Consonant
		if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
				|| ch == 'O' || ch == 'U') {
			System.out.println("The given character is a Vowel.");
		} else {
			System.out.println("The given character is a Consonant.");
		}

	}

}

OUTPUT:

Enter an alphabet character:
X
The given character is a Consonant.

NOTE:

In the above two examples, assumes that the user will enter an alphabet character only. If the user enters a non-alphabetic character, it displays the character is a consonant which is wrong.

To fix this, first, we will check whether the given character is an alphabetic character or not using isAlphabetic() method. It is a method available in the Character class in Java.

Example-3. if the user enters a non-alphabetic character

In this example, if the user enters a non-alphabetic character, it will print error message “Error! Non-alphabetic character.”

package com.javacodepoint.basics;

import java.util.Scanner;

public class CheckVowelConsonant3 {

	public static void main(String[] args) {
		
		// Create scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an alphabet character: ");
		char ch = sc.next().charAt(0);

		if (!Character.isAlphabetic(ch)) {
			// Non-alphabet character
			System.out.println("Error! Non-alphabetic character.");
		} else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
				|| ch == 'O' || ch == 'U') {
			// Vowel
			System.out.println("The given character is a Vowel.");
		} else {
			// Consonant
			System.out.println("The given character is a Consonant.");
		}

	}

}

OUTPUT:

Enter an alphabet character:
5
Error! Non-alphabetic character.

Example-4. Check whether a Character is a Vowel or Consonant using switch case

The switch expression is evaluated once. The value of the expression is compared with the values of each case.

package com.javacodepoint.basics;

import java.util.Scanner;

public class CheckVowelConsonant4 {

	public static void main(String[] args) {
		// Create scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an alphabet character: ");
		char ch = sc.next().charAt(0);

		if (!Character.isAlphabetic(ch)) {
			// Non-alphabet character
			System.out.println("Error! Non-alphabetic character.");
			return;
		}

		boolean isVowel = false;

		// Check vowel using switch case
		switch (ch) {
		case 'a':
		case 'e':
		case 'i':
		case 'o':
		case 'u':
		case 'A':
		case 'E':
		case 'I':
		case 'O':
		case 'U':
			isVowel = true;
		}

		// Print the result
		if (isVowel) {
			System.out.println("The given character is a Vowel.");
		} else {
			System.out.println("The given character is a Consonant.");
		}

	}

}

OUTPUT:

Enter an alphabet character:
U
The given character is a Vowel.

Example-5. find Vowel or Consonant using a String with all Vowel Characters.

In this example, we will take a String with all vowel characters ie- “aeiouAEIOU“, and check whether the given character is available in this string or not.

package com.javacodepoint.basics;

import java.util.Scanner;

public class CheckVowelConsonant5 {

	public static void main(String[] args) {
		// Create scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an alphabet character: ");
		char ch = sc.next().charAt(0);

		// All Vowels (lowercase and uppercase)
		String allVowels = "aeiouAEIOU";

		// Check given character is an alphabet or not
		if (Character.isAlphabetic(ch)) {

			// Check the character available in allVowels
			if (allVowels.indexOf(ch) != -1) {
				System.out.println("The character " + ch + " is a Vowel.");
			} else {
				System.out.println("The character " + ch + " is a Consonant.");
			}
		} else {
			// Non-alphabet character
			System.out.println("Error! " + ch + " is a Non-alphabetic character.");
		}
	}

}

OUTPUT:

Enter an alphabet character:
y
The character y is a Consonant.

Example-6. find if a character is a vowel or Consonant using the ternary operator (? :)

In this example, we use the ternary operator (? :) to find whether a given character is a Vowel or Consonant.

package com.javacodepoint.basics;

import java.util.Scanner;

public class CheckVowelConsonant6 {

	public static void main(String[] args) {
		// Create scanner object to read user input
		Scanner sc = new Scanner(System.in);

		System.out.println("Enter an alphabet character: ");
		char ch = sc.next().charAt(0);

		// Check whether a character is Vowel or not
		boolean isVowel = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E'
				|| ch == 'I' || ch == 'O' || ch == 'U') ? true : false;

		// Print the result
		if (isVowel)
			System.out.println("The given character is a Vowel.");
		else
			System.out.println("The given character is a Consonant.");

	}

}

OUTPUT:

Enter an alphabet character:
e
The given character is a Vowel.

See also:
Java Program to Check Whether a Number is Even or Odd.
How to count the occurrence of the given character in a string in java?

Java logical programs list


Java Basic Programs

Java String Programs

Java String Array Programs

Java Miscellaneous Programs

Java Programs based on the Collection Framework

Java Programs based on Stream API (Java 8)

Based on Integer Collections

Leave a Reply

Your email address will not be published. Required fields are marked *