Here's an example Java program to check whether a given character is a vowel or a consonant:
import java.util.Scanner; public class VowelOrConsonant { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a character: "); char ch = scanner.next().charAt(0); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { System.out.println(ch + " is a vowel"); } else { System.out.println(ch + " is a consonant"); } } }
In this program, we use a Scanner
object to read a character from the user. We then check whether the character is one of the vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). If it is, we print a message saying that the character is a vowel. Otherwise, we print a message saying that the character is a consonant. Note that we use the charAt()
method to extract the first character from the user input.