title: Remove stop words in Java --- Help needed
body: <p>Im using a method to remove stop word defined in a file, that will rip off those words from the query string that i pass to this method... The code is working fine</p>
  
  <p>Now what i need to do is ... If the query string contains just those stop words alone then it should not be ripped of..</p>
  
  <p>eg : if the stopwords file has "is" "was" "and"</p>
  
  <p>if the query is "I was a student" then the output should be " I a student"</p>
  
  <p>but if the query is "and is " now i need the output the same as "and is".</p>
  
  <p>Below is the method that i wrote to remove stop words.</p>
  
  <pre><code>public static String removeStopWords(String  query) throws UnsupportedEncodingException
      {
        String []queryTerms = query.split("&amp;");
        String queryString="";
        StringBuffer sb =new StringBuffer();
        for (int i=0;i&lt;queryTerms.length;i++){
              if(queryTerms[i].startsWith("q=") &amp;&amp; !queryTerms[i].startsWith("q.orig")){
                  queryString = queryTerms[i].replaceAll("q=","").trim().replace("+"," ").replaceAll("\\s+"," ").trim();
                  }
  
          }
        if(!queryString.equalsIgnoreCase("")) {
        String [] tokens=queryString.split("\\s+");
        List lStopWords=StopWordDataLoad.getlQueryStringStopword();
        List&lt;String&gt; lTokens=new ArrayList&lt;String&gt;();
        boolean noStopWord=false;
        for(String s: tokens)
          if(!lStopWords.contains(s)) {
                if(sb.length()==0) sb.append(s);
                    else sb.append(" ").append(s);
            } else noStopWord=true;
  
         queryString=sb.toString().replaceAll("\\s+", " ");
         if(queryString.equalsIgnoreCase("") || noStopWord ==false) return query;
        }
        else return query;
  
  
        String fque="";
        String finQue = "";
        ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
        for (int i=0;i&lt;queryTerms.length;i++){
            if(queryTerms[i].startsWith("q=") &amp;&amp; !queryTerms[i].startsWith("q.orig")){
                fque = "q="+URLEncoder.encode(queryString,PropertyLoader.getHttpEncoding());
                list.add(fque);
  
            } else if (!queryTerms[i].equalsIgnoreCase("")) list.add(queryTerms[i]);
        }
        ListIterator&lt;String&gt; iter = list.listIterator();
          while(iter.hasNext()){
              String str = iter.next();
              finQue=finQue+"&amp;"+str;
          }
  
  
        return finQue.trim();
  
      }
  </code></pre>