- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to find whether given character is vowel or consonant using switch case
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case. To verify whether given character is a vowel read a character from the user into a variable (say ch).
Define a boolean bool variable and initialize it with false.
Define cases for character ch with vowel characters, both capital and small ('a', 'e', 'i', 'o', 'u' ) without break statements.
For all these assigns make the bool variable true.
Finally, if the value of the bool variable is true given character is a vowel else consonant
Example
import java.util.Scanner; public class VowelOrConsonantSwitch { public static void main(String args[]) { boolean bool = false; System.out.println("Enter a character :"); Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); switch(ch) { case 'A' : case 'E' : case 'I' : case 'O' : case 'U' : case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : bool = true; } if(bool == true){ System.out.println("Given character is an vowel "); }else{ System.out.println("Given character is a consonant "); } } }
Output
Enter a character : a Given character is an vowel Enter a character : l Given character is a consonant
Advertisements