Java program that determine whether a inputted character is a vowel or consonant
Posted by Samath
Last Updated: May 08, 2021

Write a program that prompts the user to provide a single character from the alphabet. Print Vowel or Consonant, depending on the user input. If the user input is not a letter (between a and z or A and Z) or is a string of length > 1, print an error message.

Code:

import java.util.Scanner;

public class Main
{
  public static void main(String[] args) {
      
	Scanner input = new Scanner(System.in);
	char c1;
	
	System.out.print("Enter character: ");
	c1 = input.next().charAt(0);
	input.close();
	
	if (Character.isLetter(c1)) {
	    if (c1 == 'a' || c1 == 'A' || c1 == 'e' || c1 == 'E'
	        || c1 == 'i' || c1 == 'I' || c1 == 'o' || c1 == 'O'
	        || c1 == 'u' || c1 == 'U') {
		System.out.println("The character is a vowel.");
	    }
	    else {
		System.out.println("The character is a consonant");
	    }
	}
	else {
	    System.out.println("Invalid input. Not a character!");
	}
    }
	
}