Java Online Compiler
Example: CheckVowelConsonantSet in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// CheckVowelConsonantSet import java.util.HashSet; import java.util.Scanner; import java.util.Set; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Create a Scanner object Scanner scanner = new Scanner(System.in); // Step 2: Prompt for character input System.out.print("Enter a character: "); char ch = scanner.next().charAt(0); // Step 3: Check if it's an alphabet if (!Character.isLetter(ch)) { System.out.println(ch + " is not an alphabet."); scanner.close(); return; } // Step 4: Create a Set of vowels (case-insensitive for comparison) Set<Character> vowels = new HashSet<>(); vowels.add('a'); vowels.add('e'); vowels.add('i'); vowels.add('o'); vowels.add('u'); // Step 5: Convert the input character to lowercase for consistent checking char lowerCaseCh = Character.toLowerCase(ch); // Step 6: Check if the lowercase character is present in the set of vowels if (vowels.contains(lowerCaseCh)) { System.out.println(ch + " is a Vowel."); } else { System.out.println(ch + " is a Consonant."); } // Step 7: Close the scanner scanner.close(); } }
Output
Clear
ADVERTISEMENTS