In this Java program, you’ll learn how to check if a character is a vowel or consonant using Java with some sample code snippets.Â
How to Check if a Character is a Vowel or Consonant?Â
Example 1:Â
RUN CODE SNIPPETimport java.util.Scanner;Â
public class Main {Â
   public static void main(String args[])Â
{Â
      System.out.println("Enter a character :");Â
      Scanner sc = new Scanner(System.in);Â
      char ch = sc.next().charAt(0);Â
      if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' ')Â
{Â
         System.out.println("The given character is a vowel");Â
      }Â
ElseÂ
{Â
         System.out.println("The given character is a consonant");Â
      }Â
   }Â
}OUTPUTÂ
Enter a character: e Given character is a vowel
The alphabets ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and the remaining are called consonants.Â
 In the above example, we are using a loop and or operator to find whether the given character is vowels are consonants.Â
Example 2:Â
RUN CODE SNIPPETpublic class Main {Â
    public static void main(String[] args) {Â
        char ch = 'r';Â
        switch (ch) {Â
            case 'a':Â
            case 'e':Â
            case 'i':Â
            case 'o':Â
            case 'u':Â
                System.out.println(ch + " is vowel");Â
                break;Â
            default:Â
                System.out.println(ch + " is consonant");Â
        }Â
    }Â
}OUTPUTÂ
r is consonant
In the above example, we are using the switch to find whether the given character is vowels are consonants.Â