Java Program to Check Whether an Alphabet is Vowel or Consonant

95
Java Program to Check Whether an Alphabet is Vowel or Consonant
Advertisement

Java is a versatile and widely used programming language known for its simplicity and readability. In this blog post, we’ll explore a simple Java program to determine whether an alphabet is a vowel or a consonant. This is a fundamental concept for beginners in Java programming and can be useful in various applications, such as text processing and language-related tasks.

Understanding Vowels and Consonants

Before we dive into the Java code, let’s briefly understand the difference between vowels and consonants.

  • Vowels: These are the letters A, E, I, O, and U. They are pronounced with an open vocal tract and are typically more sonorous than consonants.
  • Consonants: All other letters in the alphabet, excluding the vowels, are consonants. They are pronounced with some level of obstruction in the vocal tract.

Java Program to Check Vowels and Consonants using if..else statement

Here’s a simple Java program that takes a user-input alphabet and determines whether it is a vowel or a consonant:

Java – Vowel or Consonant Check
import java.util.Scanner;

public class VowelOrConsonant {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a single alphabet: ");
        char ch = Character.toLowerCase(scanner.next().charAt(0));

        if ((ch >= 'a' && ch <= 'z')) {
            // Check if it's a lowercase vowel
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                System.out.println(ch + " is a vowel.");
            } else {
                System.out.println(ch + " is a consonant.");
            }
        } else {
            System.out.println("Invalid input. Please enter a valid alphabet.");
        }
    }
}
Output – VowelOrConsonant
Enter a single alphabet: Z
z is a consonant.

In this program:

  1. The user is prompted to enter a single alphabet.
  2. The program uses if-else statements to perform the following checks:
    • First, it checks if the input character is a valid alphabet (both lowercase and uppercase).
    • If it’s a valid alphabet, it further checks if it’s a vowel or a consonant using nested if-else statements.
    • If it’s a vowel, it prints that it’s a vowel.
    • If it’s not a vowel, it prints that it’s a consonant.
    • If the input is not a valid alphabet (e.g., a number or a special character), the program prints an error message.

Java Program to Check Vowels and Consonants using switch statement

Here’s a Java program that checks whether a character is a vowel or a consonant using a switch statement:

Java – Vowel or Consonant Check using Switch
import java.util.Scanner;

public class VowelOrConsonantSwitch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a single alphabet: ");
        char ch = Character.toLowerCase(scanner.next().charAt(0));

        switch (ch) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                System.out.println(ch + " is a vowel.");
                break;
            default:
                if ((ch >= 'a' && ch <= 'z')) {
                    System.out.println(ch + " is a consonant.");
                } else {
                    System.out.println("Invalid input. Please enter a valid alphabet.");
                }
        }
    }
}
Output – VowelOrConsonantSwitch
Enter a single alphabet: a
a is a vowel.

In this program:

  1. The user is prompted to enter a single alphabet.
  2. The switch statement is used to check the input character ch against a list of cases for both lowercase and uppercase vowels.
  3. If the input character matches any of the cases, it’s considered a vowel, and the program prints that it’s a vowel.
  4. If the input character doesn’t match any of the vowel cases, it enters the default case, where it checks if the character is a valid alphabet and not a vowel. If it meets these conditions, it’s considered a consonant, and the program prints that it’s a consonant.
  5. If the input character is not a valid alphabet (e.g., a number or a special character), the program prints an error message.

You can run this program in a Java development environment as explained in the previous response. It provides the same functionality as the earlier program but uses a switch statement for checking vowels and consonants.

Running the Program

To run the program, follow these steps:

  1. Open a Java development environment like Eclipse or IntelliJ IDEA.
  2. Create a new Java project and add a class named VowelOrConsonantor VowelOrConsonantSwitchbased on the program.
  3. Copy and paste the program code into the class.
  4. Run the program.
  5. Enter a single alphabet when prompted, and the program will tell you whether it’s a vowel or a consonant.

Conclusion

This simple Java program to check whether an alphabet is a vowel, or a consonant is a great way to introduce basic input handling and conditional statements to beginners. It’s a fundamental concept that can be the building block for more complex text processing tasks in Java. Happy coding!

Latest Posts: