2013-10-08 6 views
1

아무도 내 코드에 무엇이 잘못되었는지 말해 줄 수 있습니까?자바에서 문자열을 제거하는 단어

removeWords() 함수에 문자열을 전달하려고하는데이 함수는 String에서 일부 정보를 제거합니다. 예를 들어

내가 전달하는 경우 :

함수가 반환해야

"나는 Headach 되세요"

"Headach"그러나

을, 내 기능이 작동하지 않습니다.

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)); 

        } 
        } 
       } 
      } 

     } 
} 
+0

을 반환은 무엇입니까? – Prateek

+0

현재 결과는 무엇입니까? – fasteque

+1

문자열을 변경할 수 없습니다. 메서드에서 새 String을 반환해야합니다. – GriffeyDog

답변

9
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,""); 
} 

출력 : 그것은 당신의 입력 Headach

+3

이 질문에 대한 기술적 대안으로 기술적 인 대안을 제시하고 있지만 범위가 제한되어있어 OP에 도움이되지 않을 수도 있습니다. – AndyPerfect

+1

@AndyPerfect correct. 문자열에서 여러 단어를 제거하는 훨씬 더 나은 대답은 [여기에서 찾을 수 있습니다] (http://stackoverflow.com/a/4769336/1276636). – Sufian

관련 문제