Java Program to check a character is a Vowel or Consonant
Character is a vowel or consonant using java
Here, in this section we will discuss the program to check whether the character is a vowel or consonant using java. In this program we take a character from the user then check that entered character. A character is said to be a vowel if it is one of the five following alphabet – a, e, i, o, u. All the remaining alphabets are called consonants.
Working:-
- Take character input from the user
- Check if Input is a lowercase or upper case vowel
- If yes then print vowel
- If not then print consonant
- Can also additional check if it’s a non-character item
We will discuss various methods to do the same thing.
Method 1
Now in here we will see how we can identify whether a character is a vowel or consonant using the Java programming language.Java Code
Run
public class Main { public static void main(String[] args) { char c = 'g'; //checking for vowels if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U') { System.out.println(c + " is a vowel "); // condition true input is vowel } else { System.out.println(c + " is a consonant "); // condition true input is consonant } } }
Output
g is a consonant
Method 2
The issue with the previous method was that we were not checking if the user entered a non-alphabet character like ‘3’ or ‘%’ etc. We will see an alternate approach and also handle this non-alphabet case.Java Code
Run
public class Main { public static void main (String[]args) { char c = 'f'; // show error message if c is not an alphabet // if (!isalpha (c)) // System.out.println ("Non alphabet"); if (isLowercaseVowel (c) || isUppercaseVowel (c)) System.out.println (c + " is a vowel "); else System.out.println (c + " is a consonant "); } static boolean isLowercaseVowel (char c) { // returns 1 if char matches any of below return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } static boolean isUppercaseVowel (char c) { // returns 1 if char matches any of below return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); } }
Output
f is a consonant
Method 3
The above method has two separate functions of upper case vowels and lowercase vowels we can reduce that down to one single method using an inbuilt function that converts any lowercase case charter to uppercase.Java Code
Run
public class Main { public static void main (String[]args) { char c = 'f'; // show error message if c is not an alphabet // if (!isalpha (c)) // System.out.println ("Non alphabet"); if (isVowel(c)) System.out.println (c + " is a vowel "); else System.out.println (c + " is a consonant "); } // single function for both uppercase and lowercase static boolean isVowel(char c) { // converts to uppercase if it wasn't already c = Character.toUpperCase(c); // returns true if char matches any of below return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); } }
Output
f is a consonant
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
For similar questions click on given button
import java.util.Scanner;
class vowelconsonant{
public static void main(String args[]){
char ch;
Scanner sc=new Scanner(System.in);
System.out.println(“Enter the string”);
ch=sc.next().charAt(0);
if(ch==’a’|| ch==’e’|| ch==’i’|| ch==’o’|| ch==’u’ ){
System.out.println(“Charater is Vowel”);
}
else{
System.out.println(“Character is consonant”);
}
}
}
Kindly refer to our discord community for all your technical doubts.
public class JvaProgramToCheckTheCharacterIsVowlOrNot {
public static void main(String args[])
{
String str=”B”;
str=str.toLowerCase();
for(int i=0;i<str.length();i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') {
System.out.println("String is vowel");
} else {
System.out.println("String is not vowel");
}
}
}
}
Kindly join our Discord Channel for technical queries.
String str=”aeiouAEIOU”;
Scanner sc=new Scanner(System.in);
char ch=sc.next().charAt(0);
if (str.indexOf(ch)!=-1){
System.out.println(“Vowel”);
}else{
if(ch>=’A’ && ch=’a’ && ch<='z'){
System.out.println("Constant");
}else{
System.out.println("Not Alphabet");
}
}
Hey!! Kindly join our Discord Community
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.next();
String vowels=”aeiouAEIOU”;
System.out.print(vowels. Contains(str));
}
}
Refer to our Discord for your queries, our mentors will guide you there.
public class VowelEg {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“Enter the String : “);
String string = scanner.next();
String str = string.toLowerCase();
char[] ch = str.toCharArray();
String vowels = “”;
String consonent = “”;
List characters = List.of(‘a’, ‘e’, ‘i’, ‘o’, ‘u’);
for (int i=0; i<str.length(); i++){
if (characters.contains(ch[i])){
vowels = vowels + ch[i];
}else
consonent = consonent + ch[i];
}
System.out.println("vowels = " + vowels);
System.out.println("consonent= " + consonent);
}
}
Join our Discord channel if you have any technical query <3
class HelloWorld {
public static void main(String[] args) {
char c=’b’;
if(c==’a’||c==’e’||c==’i’||c==’o’||c==’u’|| c==’A’||c==’E’||c==’I’||c==’O’||c==’U’){
System.out.println(“Vowel”);}
else{
System.out.println(“Consonent”);
}
}
}
public class VowelConsonant {
void isVowel(char input){
if (input==’a’ || input == ‘e’ || input == ‘i’ || input == ‘o’ || input == ‘u’){
System.out.println(“The character ” + input + ” is a vowel”);
}else {
System.out.println(“The character ” + input + ” is a consonant”);
}
}
public static void main(String[] args) {
VowelConsonant obj = new VowelConsonant();
obj.isVowel(‘t’);
}
}
Heyy!!
Kindly refer discord for all your doubts
import java.util.Scanner;
public class VowelOrConstant {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
char c1 = s.next().charAt(0);
char c = java. lang. Character. toLowerCase(c1);
if(c == ‘a’ || c == ‘e’ || c ==’i’ || c==’o’ || c==’u’) {
System.out.println(“Vowel”);
}else {
System.out.println(“Consonant”);
}
}
}
import java.util.Scanner;
public class vowel_or_consonant {
public static void main(String[] args) {
String v= “aeiouAEIOU”;
Scanner sc = new Scanner(System.in);
char inp= sc.next().charAt(0);
if(v.indexOf(inp)==-1)
{
System.out.println(“vowel”);
}
else
{
System.out.println(“Consonant”);
}
if((inp >= ‘A’ && inp = ‘a’ && inp <= 'z')){
System.out.println("Its not an alphabet");
}
}
}
Join our Discord for technical queries