2

Could anyone tell me what is wrong in my code?

I am trying to pass a string to function removeWords() and this function removes some information from the String.

For example if I pass:

"I Have a Headach"

the function should return:

"Headach"

However, my function is not working:

public class WordChosen extends Activity {

    private TextView wordsList;
    private String symptom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word_chosen);

        //Getting String from VoiceRecognition Activity and displaying it

        Intent intent = getIntent();
        String wordChosen = intent.getExtras().getString("wordChosen");

        //casting the string with TextView to display the result
        wordsList = (TextView) findViewById(R.id.wordChosen);

        Log.v("Word List:", "+++++"+wordChosen);
        //Setting text to be displayed in the textView

        removeWords(wordChosen);
        Log.v("removewords:", "------- message is displayed");



    }

    public void removeWords(String wordList)
    {
        ArrayList<String> stopList = null;
        stopList.add("i");
        stopList.add("have");
        stopList.add("a");

        ArrayList<String> result = new ArrayList<String>(Arrays.asList(wordList.split(" ")));
        for(int i=0; i<result.size();i++)
        {
            for(int j=0; j<stopList.size();j++)
            {

                if (result.get(i).equals(stopList.get(j))) { 
                    break;
                } 
                else { 

                   if(j==stopList.size()-1)
                    {

                       wordsList.setText(result.get(i));

                     }
                   }
                }
            }

        }
} 
4
  • What is it returning for your input? Commented Oct 8, 2013 at 20:01
  • What's you current results? Commented Oct 8, 2013 at 20:05
  • 1
    Strings are immutable. You will need to return a new String from your method. Commented Oct 8, 2013 at 20:05
  • If you want to delete specific words from your string, this answer should help you. Commented Oct 23, 2014 at 6:40

1 Answer 1

12
public static void main(String[] args) {
    String word = "I Have a Headach";
    String remove = "I Have a ";
    System.out.println(removeWords(word, remove));
}

public static String removeWords(String word ,String remove) {
    return word.replace(remove,"");
}

output : Headach

Sign up to request clarification or add additional context in comments.

2 Comments

While I guess this technically offers an alternative solution to the question asked, it has limited scope and probably isn't too helpful to OP.
@AndyPerfect correct. A much better answer (to remove multiple words from string) can be found here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.