Java Program to check whether input character is vowel or consonant

Java Program to Check Vowel or Consonant using Switch Case

Vowels are A,E,I,O and U (Small case and Upper case) except this, rest are known as consonants.

Here will write java program to check whether the input character is vowel or Consonant using Switch case in Java.

If you are new to Java learn refer the oops concepts and other Java program.

If this Program, Switch Case means if the user enters any vowel, the program will continue to execute all the subsequent cases until ‘Case U’ is reached it is not breaking the statement. By setting up the value of a Boolean variable to true, this way we can identify that the alphabet entered by the user is a vowel or not.

We are using Scanner class, that is user define inputs, in this example.


import java.util.Scanner;
class CheckVowelConsonants
{
   public static void main(String[ ] arg)
   {
	boolean isVowel=false;;
	Scanner scanner=new Scanner(System.in);
	System.out.println("Enter a character : ");
	char ch=scanner.next().charAt(0); 
	scanner.close();
	switch(ch)
	{
	   case 'a' :
	   case 'e' :
    	   case 'i' :
	   case 'o' :
	   case 'u' :
	   case 'A' :
	   case 'E' :
	   case 'I' :
	   case 'O' :
	   case 'U' : isVowel = true;
	}
	if(isVowel == true) {
	   System.out.println(ch+" is  a Vowel");
	}
	else {
	   if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		System.out.println(ch+" is a Consonant");
	   else
		System.out.println("Input is not an alphabet");		
        }
   }
}


Enter a character : 
U
U is  a Vowel

Output 2:

Enter a character : 
M
M is a Consonant

Output 3:

Enter a character : 
7
Input is not an alphabet

Eclipse code:


===================



Previous
Next Post »