-2

For my program I am trying to replace all the vowels in a word with asterisk. I have written the code but keep receiving an error in the line where I am trying to replace the letter. The error I receive is " cannot find symbol" can someone explain to me why I am receiving this error?

for(int index =0; index<=length;index++){
    Character vowel = firstName.charAt(index);
  if ((vowel == 'A') || (vowel == 'a') || (vowel == 'E') || (vowel == 'e') || (vowel == 'I') || (vowel == 'i')
      || (vowel == 'O') || (vowel == 'o') || (vowel == 'U') || (vowel == 'u')){
         vowel = vowel.replace( vowel, '*'); // error received here 
        } 
    }
lyah
  • 77
  • 6
  • what exactly is length? – Gianlucca Nov 29 '17 at 17:06
  • length is the length of my string – lyah Nov 29 '17 at 17:07
  • How do you think your code works? Can you describe it step by step? What do you think `vowel = vowel.replace( vowel, '*');` should do exactly (what effects should it give, should it affect somehow `firstName`)? – Pshemo Nov 29 '17 at 17:10
  • I think that my code reads the first letter of the string ``first name` Then if a the letter is a vowel the letter will then be replaced by a `* ` This is what I think `vowel = vowel.replace( vowel, '*'); `should do @Pshemo – lyah Nov 29 '17 at 17:13
  • Strings in Java are immutable (which means you can't change them). Also `vowel` is of type `Character` and there is no `replace` method in that class (which is why you are getting this error). Maybe you wanted to use `firstName.replace(....)` since it looks like `firstName` is String which has such method. But still `replace` method in String class doesn't really change original string, it creates *new* string with changed content. Also it replaces *all* places which matches targeted part so `"abcabc".replace('a','*')` will result in `"*bc*bc"` not `"*bcabc"`. – Pshemo Nov 29 '17 at 17:19
  • The actual error you get is `cannot find symbol \n symbol: method replace(java.lang.Character,char) \n location: variable vowel of type java.lang.Character` -- that's the compiler's way of saying "The object `vowel` hasn't got a method called `replace(Character, char)` but you're trying to call one" – slim Nov 29 '17 at 17:20
  • Maybe (1) take all characters from original strings and placing them in `char[]` array (2) iterate over all characters in that array (3) if you find vowel in that array replace it with `*` (4) build new String from (potentially) modified array. – Pshemo Nov 29 '17 at 17:24

3 Answers3

1

why don't you just do :

   firstName = firstName.replace(firstName.charAt(index), '*');

or

   firstName = firstName.replace(vowel, '*');
C.Ounoughi
  • 36
  • 4
0

vowel is of typ Character and does not have a .replace method. Not exactly sure what you want to achive but you might want to replace the char in firstname:

 firstname.replace( vowel, '*'); 
andgalf
  • 170
  • 10
0

I would prefer a single regular expression to your loop logic, and you might use ?i to ignore case. Basically, match any vowel and replace with an asterisk. Like,

String firstName = "David";
System.out.println(firstName.replaceAll("(?i)[aeiou]", "*"));

Outputs

D*v*d
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249