I’m having a bit of trouble figuring out how to remove certain words from a string. Basically I have a String. I compare every word in the string to a preset number of words I have in an array. If a word in the string matches one of the preset words I remove that word from the string.
As an example I have the string “is a test sentence”, after running the method I should have an array with the words {“test”, “sentence”} Here’s what I have thus far…
edit
Basically the issue is that nothing changes, I end up with {“is”, “a”, “test”, “sentence”}
private void fillerWords(){
String[] commonWords = {"the","of","to","and","a","in","is","it","you","that","he","was","for","on","are","with","as","i"};
List <String>wordList = new ArrayList<String>(Arrays.asList(commonWords));
//Split words in sentence up by word, put them into array
String s = "is a test sentance";
String[] tArray = s.split(" ");
List <String>list = new ArrayList<String>(Arrays.asList(tArray ));
//take out words
for(int i=0; i<list.size(); i++){
//Check to see if a sentence word is a common word, if so remove word
for(int c=0; c<wordList.size(); c++){
if(wordList.get(c) == list.get(i)){
list.remove(i);
}//end if
}//end for
}//end for
for(int x=0; x<list.size(); x++){
System.out.printf("%s %s \n", x, list.get(x));
}
}
}
The problem is that you are removing index i from the list and then incrementing i, so you are skipping one every time you remove. Maybe create another list called output and instead of removing from “list” when you hit a bad word, just add to “output” when you hit a good word.
Also, as Failsafe said, you can’t use “==” to compare strings, you need to use string1.equals(string2) to compare.
Also, here’s a short way to fix it without changing much:
Change your compare block as such: