Java Program to Check Whether an Alphabet is Vowel or Consonant [if-else & switch-case]

Updated on February 24, 2025
Check Whether an Alphabet is Vowel or Consonant [if-else & switch-case] header image

Introduction

In Java programming, determining whether a given alphabet is a vowel or consonant is a basic task that helps novices understand control flow statements like if-else and switch-case. This concept is fundamental in building the foundational skills of handling conditional logic in programming.

In this article, you will learn how to check whether character is vowel or consonant in java. First, delve into implementing this with if-else statements and then using switch-case for a comparison. Each method will be accompanied by a detailed example and explanation.

Java Program to Check Vowel or Consonant Using if-else Statements

Check Vowel or Consonant with if-else

  1. Start by setting up a Java class and the main method.

  2. Read a character input from the user.

  3. Implement if-else logic to determine if the input is a vowel or consonant.

    java
    import java.util.Scanner;
    
    public class VowelConsonantChecker {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a single alphabet: ");
            char ch = scanner.next().toLowerCase().charAt(0);
    
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                System.out.println(ch + " is a vowel.");
            } else if ((ch >= 'a' && ch <= 'z')) {
                System.out.println(ch + " is a consonant.");
            } else {
                System.out.println("Input is not a valid alphabet.");
            }
        }
    }
    

    This code includes a Scanner object to capture user input, converts it to lowercase to handle case sensitivity, and uses if-else statements to determine if the character is a vowel, a consonant, or an invalid input.

Check Whether Character is Vowel or Consonant in Java Using switch-case Statements

Implementing switch-case for Vowel Check

  1. Create another Java class with a different input approach if desired.

  2. Use a switch-case statement to check the character directly.

    java
    import java.util.Scanner;
    
    public class VowelConsonantSwitch {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("Enter a single alphabet: ");
            char ch = scanner.next().toLowerCase().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("Input is not a valid alphabet.");
                    }
            }
        }
    }
    

    This implementation leverages switch-case for checking vowels. For consonant identification and error handling, an additional check is used in the default case to manage non-alphabet characters.

Java Program to Check Whether an Alphabet is Vowel or Consonant using Scanner

Here's a Java program using the Scanner class to check whether an alphabet is a vowel or consonant:

java
import java.util.Scanner;

 public class VowelOrConsonant {
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
     
         System.out.print("Enter an alphabet: ");
         char ch = scanner.next().charAt(0);
     
         if (Character.isLetter(ch)) {
             ch = Character.toLowerCase(ch);
         
             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("Please enter a valid alphabet.");
         }
     
         scanner.close();
     }
 }

Java Program to Find Vowels and Consonants in a String

Here's a Java program to find the vowels and consonants in a string:

java
import java.util.Scanner;

 public class VowelsAndConsonantsInString {
     public static void main(String[] args) {
 
         Scanner scanner = new Scanner(System.in);
     
  
         System.out.print("Enter a string: ");
         String str = scanner.nextLine();
     
   
         int vowelsCount = 0;
         int consonantsCount = 0;
     

         str = str.toLowerCase();
     
     
         for (int i = 0; i < str.length(); i++) {
             char ch = str.charAt(i);
         
       
             if (Character.isAlphabetic(ch)) {
        
                 if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                     vowelsCount++;
                 } else {
                     consonantsCount++;
                 }
            }
        }
     

         System.out.println("Total vowels: " + vowelsCount);
         System.out.println("Total consonants: " + consonantsCount);
     

         scanner.close();
     }
 }

Conclusion

Both if-else and switch-case are excellent control structures for handling decision-making processes in Java. While if-else offers more flexibility for complex conditions, switch-case provides a more readable format when dealing with a well-defined set of values, such as checking for vowels. By mastering these examples, enhance your ability to manage simple yet crucial decision-making processes in your Java applications.