Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check how many consonants and vowels there are in a sentence in Java?

Tags:

java

loops

char

At the end of my loop, I am planning on displaying the number of consonants and vowels in the sentence. I was wondering if there was a more efficient way to check how many consonants and vowels are in a given sentence, rather than using an if statement and manually inputting every letter. (key refers to my Scanner which has already been initialized)

Edit: It needs to ignore digits and other special characters, so for example if I write Hello@ how 1are you?. There should be 8 vowels and 6 consonants.

System.out.println("Please enter the sentence to analyze: ");

String words = key.nextLine(); //the sentence the user inputs
int c = 0; //# of consonants
int v = 0; //# of vowels
int length = words.length(); //length of sentence
int check; //goes over each letter in our sentence

for(check = 0; check < length; check++){
char a = words.charAt(check);   

        if(a == 'a' || a == 'A' || a == 'e' || a == 'E' || a == 'i' || a == 'I' || a == 'o'
            || a == 'O' || a == 'u' || a == 'U' || a == 'y' || a == 'Y')
            v = v + 1;
        else if(a == 'b' || a == 'B' || a == 'c' || a == 'C' || a == 'd' || a == 'D' || a == 'f'
            || a == 'F' || a == 'g' || a == 'G' || a == 'h' || a == 'H' || a == 'j' || a == 'J' 
            || a == 'k' || a == 'K' || a == 'l' || a == 'L' || a == 'm' || a == 'M' || a == 'n' 
            || a == 'N' || a == 'p' || a == 'P' || a == 'q' || a == 'Q' || a == 'r' || a == 'r'
            || a == 's' || a == 'S' || a == 't' || a == 'T' || a == 'v' || a == 'V' || a == 'w'
            || a == 'W' || a == 'x' || a == 'X' || a == 'z' || a == 'Z')
                c = c + 1;
}
like image 833
t3rrh42d2 Avatar asked Dec 11 '22 05:12

t3rrh42d2


2 Answers

Use Character.isLetter(ch) to determine if the character is a vowel or a consonant, then check to see if the character in question is in the set of vowels.

One way to create the set of vowels:

    Set<Character> vowels = new HashSet<Character>();
    for (char ch : "aeiou".toCharArray()) {
        vowels.add(ch);
    }

And to increment v or c:

    if (Character.isLetter(a)) {
        if (vowels.contains(Character.toLowerCase(a))) {
            v++;
        } else {
            c++;
        }
    }
like image 69
user3634796 Avatar answered Apr 07 '23 23:04

user3634796


Assuming you already have a letter (vowel or consonant, not a digit nor a symbol or anything else), then you can easily create a method to define if the letter is a vowel:

static final char[] vowels = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U', 'y', 'Y' };

public static boolean isVowel(char c) {
    for (char vowel : vowels) {
        if (c == vowel) {
            return true;
        }
    }
    return false;
}

public static boolean isConsonant(char c) {
    return !isVowel(c);
}

Note that I set Y and y as vowels since seems that they are in your language. In Spanish and English, Y is a consonant (AFAIK).

You can easily check if the char is a letter or not using Character#isLetter.

So, your code would become into:

for(check = 0; check < length; check++){
    char a = words.charAt(check);
    if (Character.isLetter(a)) {
        if (isVowel(a)) {
            v++;
        } else {
            c++;
        }
    }
}
like image 38
Luiggi Mendoza Avatar answered Apr 08 '23 00:04

Luiggi Mendoza