vowels = wordcount.vowelNum(); //counts the vowels
consonants = wordcount.consNum(); //counts the consonants, this is not working properly.

the consNum() method:

int length = word.length();

		for(int i = 0; i < length; i++){
			if(word.charAt(i) == 'a' || word.charAt(i) == 'u'|| word.charAt(i) == 'i'|| word.charAt(i) == 'e'|| word.charAt(i) == 'o'){
			vowelCount++;
		}
	}
		
	consCount = (word.length() - vowelCount);
	return consCount;

the above method is giving me weird results, for example if i typed in "dexter" and it would say it has 2 consonants. the vowel count method, however, works.

Recommended Answers

All 3 Replies

Where are you initializing vowelCount? Anyway, you could just reuse the vowelNum method and do something like this: return word.length() - vowelNum();

are you trying to have the same method counting both consonants and vowels? this method you provided does not return the number of vowels. it does count the number of vowels, but it does not return it.

ok nevermind i fixed it by editing the consNum() method to accept an argument which is the number of vowels (provided from the vowelsNum() method), and it works that way.

but i still don't understand why the original method didn't work. the consNums() method only returns the number of consonants and NOT the number of vowels (that's the vowelsNum method's job). i intended for it to just calculated the # of vowels, so it can figure out the # of consonants.

and death oclock, your method works too, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.