Java switch statement check if the letter is a vowel or consonant

Question

We would like to write a program that prompts the user to enter a letter.

Check whether the letter is a vowel or consonant.

Here is a sample run:

Enter a letter: B 
B is a consonant 

Enter a letter grade: a 
a is a vowel 

Enter a letter grade: # 
# is an invalid input 
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter a letter
    System.out.print("Enter a letter: ");
    String s = input.nextLine();/*  w w w  . j  a  v  a 2  s .co  m*/
    char ch = s.charAt(0);

    if (Character.isLetter(ch))
    {
      //your code here
    }
    else
      System.out.println(ch + " is an invalid input");
  }
}




import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter a letter
    System.out.print("Enter a letter: ");
    String s = input.nextLine();
    char ch = s.charAt(0);

    if (Character.isLetter(ch))
    {
      switch(Character.toUpperCase(ch))
      {
        case 'A': 
        case 'E': 
        case 'I': 
        case '0': 
        case 'U': System.out.println(ch + " is a vowel"); break;
        default : System.out.println(ch + " is a consonant"); 
      }
    }
    else
      System.out.println(ch + " is an invalid input");
  }
}



PreviousNext

Related