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
- Create a method called ‘Vowel_Or_Consonant’ that accepts the character ‘y’ as input.
- Within the method, determine whether the character ‘y’ equals ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘A’, ‘E’, ‘I’, ‘O’, or ‘U’.
- Print "It is a Vowel" if the character matches any of the vowels.
- If not, print "It is a Consonant."
- Specify the main method.
- 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
- Create a class called "Prep."
- Create the ‘main’ method, which accepts an array of strings ‘args’ as input.
- Create a variable ‘ch’ of type ‘char’ and set its value to ‘z’.
- Begin a switch statement with the expression ‘ch’.
- Define cases for ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ within the switch statement.
- If the character ‘ch’ matches any of the cases, print "’ch’ is a vowel" and exit the switch statement.
- If ‘ch’ does not match any of the cases, the default case is executed.
- Within the default case, print "’ch’ is a consonant."
- Put an end to the switch statement.
- 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
-
Define a class named
Prep
. -
Define a static method named
isVowel
that takes a characterch
as input and returns a string. -
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 theString
class to check if the characterch
is present in thestr
string. - If the
indexOf
method returns a value not equal to -1, it means the character is present instr
, so return "Vowel". - If the
indexOf
method returns -1, it means the character is not present instr
, so return "Consonant".
- Declare a string variable
-
Define the
main
method that takes an array of stringsargs
as input. -
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’.
- Print "It is a " followed by the result of the
- Print "It is a " followed by the result of the
-
End the
main
method. -
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.