I am doing an exercise from my book but it only works partially. It works for one of the three words that I want to censor. I have no idea why it works like that. Here is the code:
public static void main(String[] args){
String text = "Microsoft announced its next generation Java compiler today. It uses advanced parser and special optimizer for the Microsoft JVM.";
String forbiddenWords = "Java,JVM,Microsoft";
String[] words = forbiddenWords.split(",");
String newText = "";
for(String word: words){
System.out.println(word);
}
for(int i = 0; i < words.length; i++){
newText = text.replaceAll(words[i], "***");
}
System.out.println(newText);
}
And this is what I get for an answer:
*** announced its next generation Java compiler today. It uses advanced parser and special optimizer for the *** JVM.
I also have to censor it with the correct amount of *
but I don't know how. I know that I am able to get the count of *
s by using words[i].length
but I don't know how to utilize it.