Search code examples
javawords

Removing stop words from String


class MyClass {
public static void remove_stopwords(String[] query, String[] stopwords) {
    A: for (int i = 0; i < query.length; i++) {
        B: for (int j = 0; j < stopwords.length; j++) {
             C: if (query[i].equals(stopwords[j])) { 
                    break B;
                } 
                else {
                    System.out.println(query[i]);
                    break B;
                }
            }
        } 
    }
}

For some reason this code only works correctly about halfway through the problem. It takes the first stopword out of the query, but it ignores the rest. Any help would be appreciated.


Solution

  •  class MyClass 
     {
        public static void remove_stopwords(String[] query, String[] stopwords) {
    
            A: for (int i = 0; i < query.length; i++) {
                //iterate through all stopwords
                B: for (int j = 0; j < stopwords.length; j++) {
                        //if stopwords found break
                        C: if (query[i].equals(stopwords[j])) { 
                            break B;
                        } 
                        else { 
                            // if this is the last stopword print it
                            // it means query[i] does not equals with all stopwords
                            if(j==stopwords.length-1)
                            {
                               System.out.println(query[i]);
                            }
                        }
                    }
                } 
            }
        }