Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Java Program to Check Whether a Character is Vowel or Consonant

Last Updated on May 16, 2023 by Prepbytes

The letters ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ in the English alphabet are vowels, while the remaining letters are consonants. We’ll go over how to determine whether a given letter is a vowel or a consonant. We will look at three approaches to this problem: if-else, switch statements, and the indexOf() operator.

To Check whether a Character is a Vowel or Consonant in Java using an If-else Statement

We will be looking at the algorithm first and then following the actual code using this approach.

Algorithm whether a Character is a Vowel or Consonant in Java using an If-else Statement

  1. Create a method called ‘Vowel_Or_Consonant’ that accepts the character ‘y’ as input.
  2. Within the method, determine whether the character ‘y’ equals ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, or ‘U’.
  3. Print "It is a Vowel" if the character matches any of the vowels.
  4. If not, print "It is a Consonant."
  5. Specify the main method.
  6. Test the functionality by calling the ‘Vowel_Or_Consonant’ method twice with different characters (‘W’ and ‘I’).

Code Implementation

import java.io.*;
class Prep {
    static void Vowel_Or_Consonant(char y)
    {
        if (y == 'a' || y == 'e' || y == 'i' || y == 'o'
            || y == 'u' || y == 'A' || y == 'E' || y == 'I'
            || y == 'O' || y == 'U')
            System.out.println("It is a Vowel.");
        else
            System.out.println("It is a Consonant.");
    }
    static public void main(String[] args)
    {
        Vowel_Or_Consonant('W');
        Vowel_Or_Consonant('I');
    }
}

Output:

It is a Consonant
It is a Vowel

To Check whether a Character is a Vowel or Consonant in Java using Switch Statement

We will be looking at the algorithm first and then following the actual code using this approach.

Algorithm whether a Character is a Vowel or Consonant in Java using Switch Statement

  1. Create a class called "Prep."
  2. Create the ‘main’ method, which accepts an array of strings ‘args’ as input.
  3. Create a variable ‘ch’ of type ‘char’ and set its value to ‘z’.
  4. Begin a switch statement with the expression ‘ch’.
  5. Define cases for ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ within the switch statement.
  6. If the character ‘ch’ matches any of the cases, print "’ch’ is a vowel" and exit the switch statement.
  7. If ‘ch’ does not match any of the cases, the default case is executed.
  8. Within the default case, print "’ch’ is a consonant."
  9. Put an end to the switch statement.
  10. Close the ‘main’ method.

Code Implementation

class Prep {
    public static void main(String[] args) {
        char ch = 'z';
        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:

z is consonant

To Check whether a Character is a Vowel or Consonant in Java using indexOf() Operator

We will be looking at the algorithm first and then following the actual code using this approach.

Algorithm whether a Character is a Vowel or Consonant in Java using indexOf() Operator

  1. Define a class named Prep.

  2. Define a static method named isVowel that takes a character ch as input and returns a string.

  3. Inside the method:

    • Declare a string variable str and assign it the value "aeiouAEIOU". This string contains all the vowels.
    • Use the indexOf method of the String class to check if the character ch is present in the str string.
    • If the indexOf method returns a value not equal to -1, it means the character is present in str, so return "Vowel".
    • If the indexOf method returns -1, it means the character is not present in str, so return "Consonant".
  4. Define the main method that takes an array of strings args as input.

  5. Inside the main method:

    • Print "It is a " followed by the result of the isVowel method called with the character ‘a’.
      • Print "It is a " followed by the result of the isVowel method called with the character ‘x’.
  6. End the main method.

  7. End the class definition.

Note: The algorithm described above is a high-level description of the existing code. The code provided already implements the algorithm.

Code Implementation

import java.io.*;
class Prep {
    static String isVowel(char ch)
    {
        String str = "aeiouAEIOU";
        return (str.indexOf(ch) != -1) ? "Vowel" : "Consonant";
    }
    public static void main(String[] args)
    {
        System.out.println("It is a " + isVowel('a'));
        System.out.println("It is a " + isVowel('x'));
    }
}

Output

It is a Vowel
It is a Consonant

Conclusion
Finally, the article looks at three methods for determining whether a given letter in Java is a vowel or a consonant. The if-else statement, switch statement, and indexOf() operator are all discussed, with each providing a unique method of character. The code examples show how these approaches work by checking various characters and printing the results. These approaches offer flexibility and can be chosen based on specific coding preferences and requirements.

Frequently Asked Questions (FAQs)

Q1. What exactly is character in Java?
Ans. A value of the primitive type char is wrapped in an object by the Character class. A single field of type char is present in a character object.

Q2. Is a character a letter Java?
Ans. A character is considered a Java letter if it is a letter, the dollar sign "$," or an underscore "_."

Q3. In Java, how do you enter characters?
Ans. Next() is the best and most straightforward alternative to taking char input in Java. charAt(0). The charAt(0) command is used in conjunction with the straightforward next() command, which instructs Java to record the next character or string entered into the command line.

Leave a Reply

Your email address will not be published. Required fields are marked *