Wednesday, January 21, 2015

Input a letter and check whether it is vowel or not

This java program will check a character if it is vowel or not. I have used the switch case statement to check the character/letter whether it is vowel or consonant.

After running this java program, you will be asked to provide a letter. When you will input a letter using your keyboard, the the letter you have recently input will be stored into a variable called letter here.

Later this stored letter will be compared in the cases.
switch case,vowel or not,checking a letter

import java.util.Scanner;

public class VowelConsonant{
 public static void main(String [] args){
  //delaring a string type variable
  String letter;

  //getting input from keyboard
  Scanner in = new Scanner(System.in);
  System.out.print("Enter a letter: ");
  letter = in.nextLine();

  //comparing the letter with the cases if match found
  switch(letter){
   case "a":
   case "A":
   System.out.println("This is a vowel");
   break;

   case "e":
   case "E":
   System.out.println("This is a vowel");
   break;

   case "i":
   case "I":
   System.out.println("This is a vowel");
   break;

   case "o":
   case "O":
   System.out.println("This is a vowel");
   break;

   case "u":
   case "U":
   System.out.println("This is a vowel");
   break;

   //if no matches, declaring as consonant
   default:
   System.out.println("This letter is consonant");
   break;
  }
 }
}


No comments:

Post a Comment