Search code examples
androidstringstop-wordsremoveall

Removing Stopwords from String


I need to remove stopwords from a string. I use the following code to remove stopwords and setting the final output in a textView. But when i run the code it always give the output "bugs". In other words it always give me the last string word as output. Please Check my code and Help!

public class Testing extends Activity {
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testing);
    t1= (TextView)findViewById(R.id.textView1);
    String s="I love this phone, its super fast and there's so" +
            " much new and cool things with jelly bean....but of recently I've seen some bugs.";
    String[] words = s.split(" ");
    ArrayList<String> wordsList = new ArrayList<String>();
    Set<String> stopWordsSet = new HashSet<String>();
    stopWordsSet.add("I");
    stopWordsSet.add("THIS");
    stopWordsSet.add("AND");
    stopWordsSet.add("THERE'S");

    for(String word : words)
    {
        String wordCompare = word.toUpperCase();
        if(!stopWordsSet.contains(wordCompare))
        {
            wordsList.add(word);
        }
    }

    for (String str : wordsList){
        System.out.print(str+" ");
        t1.setText(str);
    }
}

Solution

  • t1.setText(str); means it doesn't care what the previous text was. It puts the last one in loop. So use append instead.

    t1.append(str);
    

    OR Append every single str to a single String and set that in TextView after the loop.