Java Program to check a character is a Vowel or Consonant

Character is a vowel or consonant using java

Here, in this section we will discuss the program to check whether the character is a vowel or consonant using java. In this program we take a character from the user then check that entered character. A character is said to be a vowel if it is one of the five following alphabet – a, e, i, o, u. All the remaining alphabets are called consonants.

character is a vowel or consonant using java

Working:-

  • Take character input from the user
  • Check if Input is a lowercase or upper case vowel
  • If yes then print vowel
  • If not then print consonant
  • Can also additional check if it’s a non-character item

We will discuss various methods to do the same thing.

Character is a vowel or consonant

Method 1

Now in here we will see how we can identify whether a character is a vowel or consonant using the Java programming language.

Java Code

Run
public class Main
{
    public static void main(String[] args)
    {

        char c = 'g';   

        //checking for vowels    
        if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
        {
            System.out.println(c + " is a vowel ");  // condition true input is vowel
        }
        else
        {
            System.out.println(c + " is a consonant ");  // condition true input is consonant
        }

    }

}

Output

g is a consonant

Method 2

The issue with the previous method was that we were not checking if the user entered a non-alphabet character like ‘3’ or ‘%’ etc. We will see an alternate approach and also handle this non-alphabet case.

Java Code

Run
public class Main
{
    public static void main (String[]args)
    {
        char c = 'f';

        // show error message if c is not an alphabet
        // if (!isalpha (c))
        //   System.out.println  ("Non alphabet");

        if (isLowercaseVowel (c) || isUppercaseVowel (c))
            System.out.println (c + " is a vowel ");

        else
            System.out.println (c + " is a consonant ");

    }

    static boolean isLowercaseVowel (char c)
    {
        // returns 1 if char matches any of below
        return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    }

    static boolean isUppercaseVowel (char c)
    {
        // returns 1 if char matches any of below
        return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    }

}

Output

f is a consonant

Method 3

The above method has two separate functions of upper case vowels and lowercase vowels we can reduce that down to one single method using an inbuilt function that converts any lowercase case charter to uppercase.

Java Code

Run
public class Main
{
    public static void main (String[]args)
    {
        char c = 'f';

        // show error message if c is not an alphabet
        // if (!isalpha (c))
        //   System.out.println  ("Non alphabet");

        if (isVowel(c))
            System.out.println (c + " is a vowel ");

        else
            System.out.println (c + " is a consonant ");

    }

    // single function for both uppercase and lowercase
    static boolean isVowel(char c)
    {
        // converts to uppercase if it wasn't already
        c = Character.toUpperCase(c);
    
        // returns true if char matches any of below
        return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    }

    
}

Output

f is a consonant

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

For similar questions click on given button

21 comments on “Java Program to check a character is a Vowel or Consonant”


  • sumitkhetre20

    package Strings;
    import java.util.*;

    public class vowel {
    public static void main(String[] args) {

    Scanner sc=new Scanner (System.in);
    System.out.println(“enter a character”);

    char ch = sc.next().charAt(3);
    if ((ch==’A’||ch==’a’)||(ch==’E’||ch==’e’)||(ch==’I’||ch==’i’)||(ch==’O’||ch==’o’)||(ch==’U’||ch==’u’))
    {
    System.out.println(“entered character is vowel”);
    }
    else
    {
    System.out.println(“entered character is consonant”);

    }
    }