'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.
import java.util.*;
class Vowels {
String s;
Set<String> set = new HashSet<String>();
Vowels(String s, LinkedHashSet<String> set) {
this.s = s;
this.set = set;
}
public void evaluator() {
Collections.addAll(set, s.split(""));
int vn = 0;
String vowel = "";
List<String> vowels = new ArrayList<String>();
Iterator<String> it = set.iterator();
while(it.hasNext()) {
switch(it.next()) {
case "a":
case "e":
case "i":
case "o":
case "u":
case "y":
case "w":
vn++;
vowel = it.next();
vowels.add(vowel);
}
}
System.out.println("Number of vowels is " + vn);
System.out.println("Vowels are " + vowels);
}
}
public class ExXVI {
public static void main(String[] args) {
LinkedHashSet<String> set1 = new LinkedHashSet<String>();
Vowels q = new Vowels("Some words have vowels, some do not", set1);
q.evaluator();
}
} /* Output:
Number of vowels is 4
Vowels are [m, , r, v]
*///:~
Solution 1:[1]
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.
Solution 2:[2]
The reason why you are missing some vowels is that you are using a Set which does not allow duplicate entries. Besides that you also call twice it.next() and thus losing the previous entry. I modified your code a bit by removing the Sets but still using your concept.
class Vowels {
String s;
Vowels(String s) {
this.s = s;
}
public void evaluator() {
int vn = 0;
String vowel = "";
List<String> vowels = new ArrayList<>();
List<String> vowelsList = Arrays.asList(s.split(""));
Iterator<String> it = vowelsList.iterator();
while (it.hasNext()) {
vowel = it.next();
switch (vowel) {
case "a":
case "e":
case "i":
case "o":
case "u":
case "y":
case "w":
vn++;
vowels.add(vowel);
}
}
System.out.println("Number of vowels is " + vn);
System.out.println("Vowels are " + vowels);
}
}
public class ExXVI {
public static void main(String[] args) {
Vowels q = new Vowels("Some words have vowels, some do not");
q.evaluator();
}
}
Will print the following:
Number of vowels is 13
Vowels are [o, e, w, o, a, e, o, w, e, o, e, o, o]
Solution 3:[3]
If you are using Java 8 you could use streams:
long numberOfVowels = input.toLowerCase().chars().mapToObj(i -> (char) i)
.filter(c -> "aeiou".contains(String.valueOf(c)))
.count();
There is a semantic problem though; 'y' is sometimes a vowel and sometimes a consonant, depending on the word it occurs in.
Solution 4:[4]
The vowels in English are [A, E, I, O, U] and sometimes Y, according to this wiki page.
I have fixed your solution:
class Vowels {
String s;
List<String> list;
Vowels(String s, List<String> list) {
this.s = s;
this.list = list;
}
public void evaluator() {
Collections.addAll(list, s.split(""));
int vn = 0;
String vowel;
List<String> vowels = new ArrayList<String>();
Iterator<String> it = list.iterator();
while(it.hasNext()) {
vowel = it.next();
System.out.println("Vowel is: " + vowel);
switch(vowel) {
case "a":
case "e":
case "i":
case "o":
case "u":
case "y":
vn++;
vowels.add(vowel);
}
}
System.out.println("Number of vowels is " + vn);
System.out.println("Vowels are " + vowels);
}
}
class ExXVI {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Vowels q = new Vowels("Some words have vowels, some do not", list);
q.evaluator();
}
}
Actual response:
Number of vowels is 11
Vowels are [o, e, o, a, e, o, e, o, e, o, o]
Be aware that Set does not allow duplicate entries, so the result of splitting the string should be stored into a List. Also, in your code, calling it.next() twice will cause your program to skip counting some vowels, as next() changes the current element by moving to right into the Collection.
Set is useful if you want to keep all unique vowels that appear in your string, so you might want to use:
Set<String> vowels = new HashSet<String>();
which results in
Number of vowels is 11
Vowels are [a, e, o]
Solution 5:[5]
long count = str.chars().filter(ch->"aeiou".contains((char)ch+"")).count();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cameronlund4 |
| Solution 2 | Murat Karagöz |
| Solution 3 | subhashis |
| Solution 4 | |
| Solution 5 | subhashis |
