Skip to content
Advertisement

Java Vowel count using Collection

I am asked to write a code to count and display vowels in a String using Sets. Any suggestions as to why the code below gives wrong output? Switch seemed like the obvious approach, I also tried with an if-statemen, but neither work properly.

JavaScript

Advertisement

Answer

I’m pretty sure it’s partially because you’re counting ‘Y’ and ‘W’ as vowels. Also, when you go to save the vowel, you call it.next() again. This actually gets the next object in the collection, not the one you just compared in the switch. You should save the value of it.next() before the switch, then use the variable to reference it later.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement