Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, December 28, 2021

Java Program to Check Whether an Alphabet is Vowel or Consonant

1. Overview

In this tutorial, you'll learn how to check the given alphabet is a vowel or consonant in java.

We are going to solve this question using if else and switch statements.

To understand the programs in this article, you should have knowledge on the following topics.






2. Java Example To check character is vowel or consonant using if else statement


In the below program first created a char variable that holds the character value 'a'.

Next, write a condition inside if condition to check the char ch is a, e, i, o, u. If ch value is matched to any one of these then the condition returns true and will enter inside if condition. It is printed onto the console using println() method.

If the ch value is not matched with if condition values then it comes to else block and prints the consonant value.

This example checks for the ch values 'a' and 'b'.

package com.javaprogramto.programs.characters;

public class IfElseVowel {

	public static void main(String[] args) {
		char ch = 'a';

		if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
			System.out.println("Chracter '" + ch + "' is a vowel");
		} else {
			System.out.println("Chracter '" + ch + "' is a consonant");
		}

		ch = 'b';

		if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
			System.out.println("Chracter '" + ch + "' is a vowel");
		} else {
			System.out.println("Chracter '" + ch + "' is a consonant");
		}

	}

}

 
Output:
Chracter 'a' is a vowel
Chracter 'b' is a consonant
 
You may have noticed that if condition is repeated twice and does not work for upper case letters.

3. Improvised code using if-else statement


Next, let us reuse the core logic for different inputs and add the support for the case sensitive.
package com.javaprogramto.programs.characters;

public class IfElseVowelImproved {

	public static void main(String[] args) {

		char ch = 'a';

		checkVowelOtNot(ch);
		checkVowelOtNot('z');
		
		// Upper case test
		checkVowelOtNot('A');
		checkVowelOtNot('X');

	}

	/**
	 * checks the given the character is vowel or not and prints the appropriate text.
	 * 
	 * @param ch
	 */
	private static void checkVowelOtNot(char ch) {
		if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
				|| ch == 'O' || ch == 'U') {
			System.out.println("Chracter '" + ch + "' is a vowel");
		} else {
			System.out.println("Chracter '" + ch + "' is a consonant");
		}

	}

} 
Output:
Chracter 'a' is a vowel
Chracter 'z' is a consonant
Chracter 'A' is a vowel
Chracter 'X' is a consonant
 

4. Example with Switch case to check vowel or not


At last look into how to check the alphabet is vowel or consonant using switch expression.
package com.javaprogramto.programs.characters;

public class SwitchVowelImproved {

	public static void main(String[] args) {

		checkVowelOtNotWithSwitch('E');
		checkVowelOtNotWithSwitch('r');

		// Upper case test
		checkVowelOtNotWithSwitch('O');
		checkVowelOtNotWithSwitch('P');

	}

	/**
	 * checks the given the character is vowel or not and prints the appropriate
	 * text.
	 * 
	 * @param ch
	 */
	private static void checkVowelOtNotWithSwitch(char ch) {
		switch (ch) {
		case 'a':
		case 'A':
		case 'e':
		case 'E':
		case 'i':
		case 'I':
		case 'o':
		case 'O':
		case 'u':
		case 'U': {

			System.out.println(ch + " is a vowel");
			break;
		}
		default:
			System.out.println(ch + " is a consonant");
			break;
		}

	}

} 

Output:
E is a vowel
r is a consonant
O is a vowel
P is a consonant

In the above program, if else condition is replaced with the switch case statement. Method checkVowelOtNotWithSwitch() takes character as input and prints saying it is vowel if is part of 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U' otherwise executes the default section.

5. Conclusion


In this article, you've seen how to check whether the given character is vowel or not using if-else and switch statements.

As usual, all examples shown are over GitHub.



No comments:

Post a Comment

Please do not add any spam links in the comments section.