This example checks whether a character entered is a vowel or a consonant. The user is first prompted to enter a letter. The input
variable is passed to the checkCharacter()
function to check the type of the letter.
import java.util.Scanner;
class VowelOrConsonant {
public static void checkCharacter(char input) {
boolean isVowel = false;
switch (input) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
isVowel = true;
}
if (isVowel) {
System.out.println(input + " is a vowel");
} else {
if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z'))
System.out.println(input + " is a consonant");
else
System.out.println("error");
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
char input;
System.out.println("Please enter a character: ");
input = scan.next().charAt(0);
scan.close();
checkCharacter(input);
}
}
Please enter a character: A
A is a vowel
Please enter a character: d
d is a consonant