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:
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.");
}
}
}
Enter a single alphabet: Z
z is a consonant.In this program:
- The user is prompted to enter a single alphabet.
- The program uses
if-elsestatements 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-elsestatements. - 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:
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.");
}
}
}
}
Enter a single alphabet: a
a is a vowel.In this program:
- The user is prompted to enter a single alphabet.
- The
switchstatement is used to check the input characterchagainst a list of cases for both lowercase and uppercase vowels. - If the input character matches any of the cases, it’s considered a vowel, and the program prints that it’s a vowel.
- If the input character doesn’t match any of the vowel cases, it enters the
defaultcase, 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. - 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:
- Open a Java development environment like Eclipse or IntelliJ IDEA.
- Create a new Java project and add a class named
VowelOrConsonantorVowelOrConsonantSwitchbased on the program. - Copy and paste the program code into the class.
- Run the program.
- 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:
- Count Subarrays with Odd SumTitle: Count Subarrays with Odd Sum In this guide, you’ll learn how to count the subarrays of an integer array… Read more: Count Subarrays with Odd Sum
- Count Subarrays Whose Product is Less than KTitle: Count Subarrays Whose Product is Less than K In this guide, you’ll learn how to count the contiguous subarrays… Read more: Count Subarrays Whose Product is Less than K
- Minimum Number of Increments to Make Array Strictly IncreasingTitle: Minimum Number of Increments to Make Array Strictly Increasing In this guide, we will solve a classic greedy array… Read more: Minimum Number of Increments to Make Array Strictly Increasing
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together



