Java Program to Check whether entered character is vowel or consonant





Below is the Program to check whether entered character is vowels or consonant.


import java.utils.*;

       public class CheckCharacter

       {

              public static void Main(string[] args)

             {

                   char c;

                   System.out.println("Please enter character");

                   Scanner sc=new Scanner(System.in);

                   c= sc.next().charAt(0);

                   if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')

                  {

                         System.out.printf("This Chatacter %c is vowel",c);

                  }

                  else

                 {

                        System.out.printf("This Chatacter %c is Consonant",c);

                  }

            }

       }



Output

Please enter character

d

This Chatacter d is a Consonant

Please enter character

a

This Chatacter d is a vowel



Explanation



  • The first sentence in the program is the import java.utils.*;.  This statement imports the required libraries and classes used for program. The utils package contains some useful classes and libraries which are required for almost all basic java programs.

  • public class CheckCharacter- In this sentence, public is the access specifier, which means , this class and it's methods are accessible to all other classess and objects. class is the keyword to declare class and the CheckCharacter  is the name of the class.

  • public static void Main(string[] args)- is the main() method which is the starting point of all Java programs. An array of strings is also passed to main() method. Without main() Method , Java programs won't compile.

  • Next , we declared character variable 'c' to store the value entered by user.

  • Further we declared Scanner Class. This class is defined in the utils package. This class contains methods to accept value entered by user on the keyboard.

  • The next sentence actually performs the function of accepting value from user. The method sc.next(). accept the value entered by user. The .charAt(0) function is used to select single character from the string. The '0' means it will select the first character in this string. This function is used here because if the user enters the string on the keyboard, the compiler will throw an error because char Data Type accepts single character. So we used charAt(0) function here because even if user enters a string, it will select only first character in the string and will proceed to execute program checking the character is vowel or consonant.

  • Next if() condition will check whether entered character is vowel or not by comparing it to the vowels which are a,e,i,o and u. If it is vowel then the next sentence will print that character on the screen saying this is vowel. '%c' is used to print the character on the screen.

  • If the entered character is not vowel , then the flow will go to else statement and print the entered character on the screen and will say the character is consonant.
Java Program to Check whether entered character is vowel or consonant Java Program to Check whether entered character is vowel or consonant Reviewed by LanguageExpert on August 20, 2017 Rating: 5

No comments