Group Switch Statement in Java


This is a Java program that uses a switch statement to check whether a given character is a vowel or a consonant. Here's how the program works:

  • The program prompts the user to enter a character by printing out "Enter The Character : ".
  • The user's character input is read in using the Scanner class and stored in the variable c.
  • The program uses a switch statement to check whether the character is a vowel or a consonant. If the character is 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', or 'U', the program prints "<character> is a Vowel". If the character is any other character, the program prints "<character> is Consonant".
  • The program ends.

Note that in the switch statement, multiple cases are grouped together to check for vowels. This is possible because the action to be taken is the same in each case (i.e., printing out that the character is a vowel). This is called "grouping" in a switch statement.

Also note that in this program, only single character input is being considered. If the user enters more than one character, only the first character will be used and the rest will be ignored.

Source Code

import java.util.Scanner;
 
//Group Switch
public class group_switch {
    public static void main(String args[]) {
       char c;
        System.out.println("Enter The Character : ");
        Scanner in =new Scanner(System.in);
        c=in.next().charAt(0);
 
        switch (c)
        {
            case 'a':
            case 'e':
            case 'i':
            case '0':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
                System.out.println(c + " is a Vowels");
                break;
            default:
                System.out.println(c + " is Consonant");
                break;
        }
 
    }
}
 

Output

Enter The Character :
u
u is a Vowels
To download raw file Click Here

Basic Programs