In Java, how do I create a simple program that prints the number of consonants and vowels in a phrase?

Here's what I have so far:

 System.out.println("CONSONANT AND VOWEL COUNTER: Please type a phrase: ");
    String lastPhrase = keyboard.nextLine();

    int countCon = 0;
    int countVow = 0;

    if (lastPhrase.contains("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")) {
        countVow++;
    }
    if (lastPhrase.contains("abcdefghijklmnopqrstuvwxyzABCDEFGHJKLIMNOPQRSTUVWXYZ")) {
        countCon++;
    }
    System.out.println("There are " + countVow + " vowels and " + countCon + " consonants.");

      

For both values, it reaches 0. What is the problem?

+3


source to share


5 answers


According to Java documentation

String contains (CharSequence s) Returns true if and only if this string contains the specified sequence of char values.



The easiest way to count the number of vowels is to loop through and check each character of the String object.

String s = "Whatever you want it to be.".toLowercase();
int vowelCount = 0;
for (int i = 0, i < s.length(); ++i) {
    switch(s.charAt(i)) {
        case 'a':
            vowelCount++;
            break;
        case 'e':
            vowelCount++;
            break;
        case 'i':
            vowelCount++;
            break;
        case 'o':
            vowelCount++;
            break;
        case 'u':
            vowelCount++;
            break;
        default:
            // do nothing
    }
}

      

+1


source


contains

searches the entire string, not individual letters.

The simplest way to do this, from the top of my head, assuming the magic String method I'm missing will manually check each character.

You have to convert the whole string to uppercase with toUpperCase

and then check if the character is an AEIOU vowel.



if(string.charAt(i) == 'A' || ... /* and so on */) {
    countVow++;
}
else {
    countCons++;
}

      

If so, add 1 to the vowels. Otherwise, add 1 to the consonants. It's either a vowel or a consonant, so if you just check those five characters, you know what it is.

Since this is probably a homework problem, I have given you a step in the right direction. You should work on a solution and come back if you need help.

+4


source


Something like that

String vowels = "aeuyio";
String consonants = "bcdfgh..."

String phrase = "amsdasmdnsn";

int vowelsCount = 0, consonantsCount = 0;
for (char ch : phrase.toCharArray()) {
    if (vowels.contains(String.valueOf(ch))) {
        ++vowelsCount;
    }

    if (consonants.contains(String.valueOf(ch))) {
        ++consonantsCount;
    }
}

      

+1


source


I would do it like this:

//convert string to lowercase
//for loop looping over the string    
if(string.charAt(i).matches([aeiou]) {
        countVow++;
}
else if(isLetter(string.charAt(I))){
        countCons++;  
} //end for loop

      

Have a look at String.matches () and regular expressions .

0


source


String.contains only works with strings and regular expressions. The fastest way to count all the consonants I can think of is:

String onlyConsonants = lastPhrase.replaceAll("[\\saeiouAEIOU0-9]", "");
countCon = onlyConsonants.length();
String onlyVowels = lastPhrase.replaceAll("[^\\saeiouAEIOU0-9]", "");
countVow = onlyVowels.length();

      

I think this solves your problem.

0


source







All Articles