Java Program to Check Whether an Alphabet is Vowel or Consonant

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Checking an Alphabet whether it is Vowel or Consonant in Java

In this article we will see multiple ways to check whether an alphabet is a vowel or consonant in Java.

We know there are total 26 alphabets in English. These letters are divided into two types of letters i.e. Vowels and Consonants.

  1. Vowels: These 5 letters ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are considered as vowel in English language.

      (Similarly capital letters ‘A’, ‘E’, ‘I’, ‘O’, ‘U’ are also vowels)

  1. Consonant: Apart from these 5 vowels, remaining other letters are considered as consonant.

And our task is to check whether the given alphabet is vowel or consonant. We will see different approaches to achieve this result.

So, let’s start one by one.

Method-1: Checking an alphabet is vowel or consonant using if-else statement:

By using if-else statement we can check whether an alphabet is vowel or consonant. Below is the approach to achieve it.

Approach:

  • Input the character from user and store it in a character variable say ‘alpha’.
  • Then check the condition if it is vowel i.e.
// condition to check whether the alphabet is vowel or not
if (alpha == ‘a’ || alpha == ‘e’ || alpha == ‘i’ || alpha == ‘o’ || alpha == ‘u’ || alpha == ‘A’ || alpha == ‘E’ || alpha == ‘I’ || alpha == ‘O’ || alpha == ‘U’)
  • This condition checks that the entered letter is matching any of these letters within the condition (if statement) or not.
  • If it satisfies then it is vowel.
  • And if the condition is not satisfied then it is consonant.

Let’s see the below program to know how actually it is working.

// Program

import java.util.*;

class Main
{
  public static void main(String[ ] args)
  {
      // Scanner class object created to take input
    Scanner sc=new Scanner(System.in);
    // Entering the character, we want to check
    System.out.println("Enter the character to check: ");
    // Asking for user input
    char alpha=sc.next().charAt(0);
    
    // Checking the the letter is vowel or not
    // If the condition satisfies then it is vowel
    if(alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u' || alpha == 'A' || alpha == 'E' || alpha == 'I' || alpha == 'O' || alpha == 'U')
    {
        // printing vowel as output
        System.out.println("It is vowel");
    }
    
    // If the condition does not satisfy then it is consonant
    else 
    {
        // printing consonant as output
        System.out.println("It is consonant");
    }
  }
}
Output:

Enter the character to check: 
V
It is consonant

// Another Case
Enter the character to check: 
e
It is vowel

Method-2: Checking an alphabet is vowel or consonant using switch statemen:

By using switch case statement, we can check whether an alphabet is vowel or consonant. Below is the approach to achieve it.

Approach:

  • Take a character input from user.
  • Here we have taken all the possible cases to check whether it is vowel or not. If input letter matches with any case, then it is vowel.
  • If the input letter does not match with any case in switch statement, then the default case is executed and it is consonant.

Let’s see the below program to know how actually it is working.

// Program :

import java.util.*;

class Main
{
  public static void main(String[ ] args)
  {
               // Scanner class object created to take input
    Scanner sc=new Scanner(System.in);
    // Entering the character we want to check
    System.out.println("Enter the character to check: ");
    // Asking for user input
    char alpha=sc.next().charAt(0);
    
    // Checking the the letter is vowel or not
    // If the entered letter matches with any case mentioned then it is vowel
    switch (alpha) 
       {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
               // input letter matched with any switch case, so it is vowel.
                System.out.println("It is vowel");
                break;
              // input letter did not match with any switch case, so default case is it is consonant
            default:
                System.out.println("It is consonant");
        }
  }
}

Output:
Enter the character to check: 
E
It is vowel

//Another Case
Enter the character to check: 
k
It is consonant

Method-3: Checking an alphabet is vowel or consonant using Nested if-else statement:

By using nested if-else statement, we can check whether an alphabet is vowel or consonant. Below is the approach to achieve it.

Approach:

  • Take a character input from user.
  • Then the input letter will be checked by multiple if statement that’s why Nested If. First if condition will check for consonant, then second if condition will check if it is vowel or not. i.e
 // checking for consonant
if((alpha>='A' && alpha <='Z')||( alpha >='a' && alpha <='z'))
// Checking for vowel
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
  • If, first if condition means outer condition is satisfied then it is consonant.
  • If, second if condition means inner condition is satisfied then it is vowel.

Let’s see the below program to know how actually it is working.

// Program :

import java.util.*;

class Main
{    
public static void main(String args[])
{
    char alpha; // local character variable 'alpha'
    Scanner sc=new Scanner(System.in);
    //Taking input from the user
    System.out.print("Enter the character to check: ");
    //asking for input from user and storing it 'alpha' variable
    alpha=sc.next().charAt(0);
    if((alpha>='A' && alpha<='Z')||(alpha>='a' && alpha<='z')){ //outer if use to check consonant 
        if(alpha =='a'||alpha =='A'||alpha=='e'||alpha =='E'||alpha=='i'||alpha =='I'||alpha =='o'||alpha =='O'||alpha =='u'||alpha =='U')
    {
        System.out.print("It is vowel"); // printing output as vowel
    }
    else 
{
    System.out.print("It is consonant"); //printing output as consonant
    }
    }
    Else
   {
    System.out.print("It is neither a vowel nor a consonant");
    }
 }
}
Output:
Enter the character to check: 
o
It is vowel

//Another Case
Enter the character to check: 
Z
It is consonant

Method-4: Checking an alphabet is vowel or consonant using using indexOf() method :

By using indexOf() method, we can check whether an alphabet is vowel or consonant. Below is the approach to achieve it.

indexOf() method : This indexOf() method takes character as parameter and it returns the index of the character passed within a particular string. And that index is the first occurrence of specified character. If the character not present in that particular String, then it returns -1.

Approach:

  • Take a character input from user.
  • Now we will pass that character to check() method, where we will check our alphabet is vowel or consonant.
  • Inside check() method, we will predefine the string “aeiouAEIOU” (This string contains all vowels)
  • And then we will check our entered character is present in this string or not by using indexOf() method.
  • If the indexOf() method returns any index then our entered character is vowel.
  • If the indexOf() method returns -1 then our entered character is not present in the string and it is a consonant.

Let’s see the below program to know how actually it is working.

// Program

import java.util.*;
  
class Main 
{
    // check() method to check character is vowel or consonant
    static String check(char ch)
    {
        // string contains all the vowel characters
        String str = "aeiouAEIOU";
        return (str.indexOf(ch) != -1) ? "Vowel" : "Consonant";
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        char alpha; // local character variable 'alpha'
        //Taking input from the user
        System.out.print("Enter the character to check: ");
        //asking for input from user and storing it 'alpha' variable
        alpha=sc.next().charAt(0);
        // calling check() method
        System.out.println("It is " + check(alpha));
    }
}
Output:

Enter the character to check: 
u
It is vowel


//Another Case
Enter the character to check: 
p
It is consonant

Method-5: Checking an alphabet is vowel or consonant using using ASCII Value:

By using ASCII method of characters also, we can check whether an alphabet is vowel or consonant. Below is the approach to achieve it.

Approach:

  • Take the user input and store it in variable ‘alpha’.
  • Here we will check ASCII code of characters in if statement.
  • Like we did in method-1, all characters we were matching, similarly we will match ASCII code of characters.
  • If the ASCII code of entered character matches with any ASCII code in if condition then it is vowel.
// ASCII codes of vowels
if(alpha ==97 || alpha ==101 || alpha ==105 || alpha ==111 || alpha ==117 || alpha ==65 || alpha ==69  || alpha ==73  || alpha ==79  || alpha ==85)
  • Otherwise, it is consonant.

Let’s see the below program to know how actually it is working.

// Program :

import java.util.*;
  
class Main 
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        char alpha; // local character variable 'alpha'
        //Taking input from the user
        System.out.print("Enter the character to check: ");
        //asking for input from user and storing it 'alpha' variable
        alpha=sc.next().charAt(0);
      // condition to check vowel or not
      // if condition contains all the ASCII code of vowels
        if(alpha==97 || alpha==101 || alpha==105 || alpha==111 || alpha==117 || 
       alpha==65 || alpha==69  || alpha==73  || alpha==79  || alpha==85)
    {
        System.out.print("It is vowel"); //printing output as vowel
    }
    else if((alpha >= 97 && alpha <= 122) || (alpha >= 65 && alpha <= 90))
{
        System.out.print("It is consonant"); //printing output as consonant
    }
    }
}
Output:
Enter the character to check: 
I
It is vowel

//Another Case
Enter the character to check: 
d
It is consonant

Method-6: Checking an alphabet is vowel or consonant using user defined method:

By using user defined method of characters also, we can check whether an alphabet is vowel or consonant. Below is the approach to achieve it.

Approach:

  • Whatever we did in all the methods using user defined method.
  • Here we can put any logic of any above methods in a user defined method.
  • Then by calling that user defined method we can check whether it is vowel or consonant.
  • During calling the user defined method we will pass the character argument.

Let’s see the below program to know how actually it is working.

// Program :

import java.util.*;

class Main
{
// User defined method check() to check entered alphabet is vowel or consonant
    void check(char ch)
    {
    //  If condition to 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("It is Vowel"); 
        }
        else 
        {
        System.out.println("It is consonant");		
    		}
    }

    public static void main(String[ ] arg)
    {
        // Main class Object created 
        Main c=new Main(); 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a character : ");
        // Taking user input
        char alpha=sc.next( ).charAt(0);	 
        // calling the check() method by passing variable ‘alpha’ as parameter		
        c.check (alpha);
    }
}
Output:
Enter the character to check: 
a
It is vowel

//Another Case
Enter the character to check: 
x
It is consonant