In Java, to check if a given character is vowel or consonant we can use conditional statements such as if else and switch statement. Here we are using switch statement. If input falls within the range of “a, e, i, o, u”, it should print vowel otherwise consonant.
In this article, we are focusing on learning through an example, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements (switch)
Problem Statement
We need to write a Java program to check given character is vowel or consonant. The program should take a character and based on the character, program will output whether the character is vowel or consonant. This action will be done in our program. Let’s look at an example,
Example:
Input: Enter the character
u
Output: u is vowel
Here for the input character u, Output is ” u is vowel”.
Logic to check if Alphabet is Vowel or Consonant
- Take character from the user using next().charAt(0) method. This method reads the next token from the input as a string and then takes the first character of that string.
- Use switch which takes character as value and compares this character with the cases (A, E, I, O, U, a, e, i, o, u) inside the switch statement.
- If any of the case matches with the switch value, it will print and break the program using break statement.
- If none of the cases matches, then using default statement we will print error message.
Java program to check if alphabet is Vowel or Consonant
In the below program, we are using switch statement to check if a alphabet is vowel or consonant in Java.
import java.util.Scanner;
public class VoewlConsonant {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the character");
char ch = s.next().charAt(0);
// character should be an alphabet only(A to Z and a to z)
switch (ch) {
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
System.out.println(ch + " is vowel");
break;
default:
System.out.println(ch + " is consonant");
}
s.close();
}
}
Output:
Enter the character
f
f is consonant
Java program explanation to check given character is Vowel or Consonant
- The Java program imports the Scanner class to read input from the user.
- In the
main
method:- It creates a Scanner object
s
to read input from the user. - Asks the user to enter a character.
- Reads the character entered by the user.
- Uses a switch statement to check the value of the character.
- It creates a Scanner object
- Inside the switch statement:
- Checks each case for the character entered.
- If the character matches any of the vowel cases (‘A’, ‘a’, ‘E’, ‘e’, ‘I’, ‘i’, ‘O’, ‘o’, ‘U’, ‘u’), it prints that the character is a vowel.
- If the character doesn’t match any of the vowel cases, it falls into the default case and prints that the character is a consonant.
- Closes the Scanner object to release system resources.
Conclusion
In this article, we have learnt how to check if a character is vowel or consonant in Java. We understood the logic and moved on to implementing the program. Hope you find this article helpful.
For more related information, you can also check our another amazing tutorials:-
- Java program to check if a character is vowel or consonant ( using if-else and ternary operator)
- Check character is vowel or consonant or special character in Java using if-else and ternary operator
- Java program to print all natural numbers in a given range
- Palindrome number program in Java
Happy Learning!