Java Count and Remove ASCII Vowels
Text filtering starts with a chosen alphabet. This exercise treats a, e, i, o and u as vowels and leaves other characters alone.
Understand the idea
Use a stated case-insensitive ASCII rule rather than claiming that the same list covers every language or accented character. A builder can accumulate the characters retained by a filter while a counter records the removed vowels. Iterating char values is appropriate for these ASCII matching decisions because the output keeps unmatched code units in their original order.
Run it locally
Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.
javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out MainA worked example
public class Main {
public static void main(String[] args) {
String text = "Mosaic tools";
StringBuilder kept = new StringBuilder();
int vowels = 0;
for (char letter : text.toCharArray()) {
if ("aeiouAEIOU".indexOf(letter) >= 0) vowels++;
else kept.append(letter);
}
System.out.println(vowels);
System.out.println(kept);
}
}
Expected output
5
Msc tlsThe counter records the five matched vowel characters and the builder preserves the others, including the space. Counting and removal share one traversal but produce different results. If the intended language or matching rule changes, revise the rule explicitly rather than adding scattered exceptions.
Check the official Java documentation for detailed contracts and supported options.