Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all vowels in a string with Java

I am doing a homework assignment for my Computer Science course. The task is to get a users input, remove all of the vowels, and then print the new statement.

I know I could easily do it with this code:

string.replaceAll("[aeiou](?!\\b)", "")

But my instructor wants me to use nested if and else if statements to achieve the result. Right now I am using something like this:

if(Character.isLetter('a')){
    'do something'
}else if(Character.isLetter('e')){
    'do something else'

But I am not sure what to do inside the if and else if statements. Should I delete the letter? Or is there a better way to do this?

Seeing as this is my homework I don't want full answers just tips. Thanks!

like image 761
Chris Frank Avatar asked Dec 01 '22 22:12

Chris Frank


1 Answers

I think what he might want is for you to read the string, create a new empty string (call it s), loop over your input and add all the characters that are not vowels to s (this requires an if statement). Then, you would simply print the contents of s.


Edit: You might want to consider using a StringBuilder for this because repetitive string concatenation can hinder performance, but the idea is the same. But to be honest, I doubt it would make a noticeable difference for this type of thing.

like image 92
arshajii Avatar answered Dec 31 '22 02:12

arshajii