Java program to find whether given character is vowel or consonant using switch case

Java Programming Java8Object Oriented Programming

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. To verify whether given character is a vowel read a character from the user into a variable (say ch).

  • Define a boolean bool variable and initialize it with false.

  • Define cases for character ch with vowel characters, both capital and small ('a', 'e', 'i', 'o', 'u' ) without break statements.

  • For all these assigns make the bool variable true.

  • Finally, if the value of the bool variable is true given character is a vowel else consonant

Example

import java.util.Scanner;
public class VowelOrConsonantSwitch {
   public static void main(String args[]) {
      boolean bool = false;
      System.out.println("Enter a character :");
      Scanner sc = new Scanner(System.in);
      char ch = sc.next().charAt(0);
      switch(ch) {
         case 'A' :
         case 'E' :
         case 'I' :
         case 'O' :
         case 'U' :
         case 'a' :
         case 'e' :
         case 'i' :
         case 'o' :
         case 'u' : bool = true;
      }
      if(bool == true){
         System.out.println("Given character is an vowel ");
      }else{
         System.out.println("Given character is a consonant ");
      }
   }
}

Output

Enter a character :
a
Given character is an vowel
Enter a character :
l
Given character is a consonant
raja
Published on 26-Apr-2018 07:17:11
Advertisements