2012-07-08 22 views
0

我正在嘗試使用嵌套循環搜索另一個用戶輸入搜索字詞數組的文本的用戶輸入數組,然後使用它們出現在文本中的次數輸出搜索字詞總文本的百分比。我認爲我在正確的軌道上,我的問題是計數器不重置每次if語句是真實的。我對編程非常陌生 - 所以我可能完全錯誤。以下是整個計劃。如果任何人都可以看一看,並幫我弄清楚我的問題是什麼,我會永遠感激。使用數組搜索字符串數組

public class termFrequency { 
    public static void main(String[] args) { 

     String searchTextPeriod, searchTextComma, searchTextApostrophe, searchTextColon, searchTextExclamation, 
     searchTextQuestion, searchText, searchTerm; 
     int counter=0, total, searchIndex=0, termIndex=0; 
     double percentage=0.0; 
     String [] searchArray, termArray; 


     searchText = JOptionPane.showInputDialog("Enter a sentence that is at least 20 words long"); 

     //removes some common punctuation from the searchable text 
     searchTextPeriod = searchText.replace(".", ""); 
     searchTextComma = searchTextPeriod.replace(",", ""); 
     searchTextApostrophe = searchTextComma.replace("'", " "); 
     searchTextColon = searchTextApostrophe.replace(":", " "); 
     searchTextExclamation = searchTextColon.replace("!", ""); 
     searchTextQuestion = searchTextExclamation.replace("?", ""); 

     searchArray = searchTextQuestion.split(" "); //splits the sentence and and puts it into an array 

     total=searchArray.length; 
     System.out.println("There are " +total +" words in your sentence"); 

     searchTerm = JOptionPane.showInputDialog("Enter your search terms here seperated by a space"); 
     termArray = searchTerm.split(" "); 
     DecimalFormat two = new DecimalFormat("#0.00"); 

     boolean found = false; 
     for (termIndex=0; termIndex<termArray.length; termIndex++) 
     { 
      for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 

       if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex])) 
       { 
        counter++; 
        found = true; 

        percentage= ((double) counter/(double)total) * 100; 
       } 
       if (found) 
        System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement."); 
       else 
        System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement."); 

      } 
     } 
    } 
} 

回答

0

您必須將「找到」中的if/else從內部循環移動到第一個循環的結尾。 您還需要重置第一個循環中的布爾值和計數器,就像您使用初始值開始分析termArray中的每個新詞一樣。

for (termIndex=0; termIndex<termArray.length; termIndex++) 
    { 
     counter=0; //Reset the counter for each word in termArray 
     found=false; //Reset the "found" flag for each word in termArray 
     for (searchIndex=0; searchIndex<searchArray.length; searchIndex++) 

      if (termArray[termIndex].equalsIgnoreCase(searchArray[searchIndex])) 
      { 
       counter++; 
       percentage= ((double) counter/(double)total) * 100; 

       found=true 
       System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement."); 
      } 
     } 
     if (found) 
       System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is found " +counter +" times. That is "+ two.format(percentage)+"% of the statement."); 
     else 
       System.out.println("Search word " + "\'" + termArray[termIndex] + "\' is not found in the statement."); 

    } 

通過你並不真的方式需要「發現」變種,現在如果counter == 0你知道這個詞已經沒有在searchArray找到。

+0

非常感謝喲!對不起,延遲迴應! – mepclu 2012-07-24 15:54:24

+0

@mepclu不客氣。遲到比從未更好;) – 2012-07-24 16:12:29

0

在第一個循環內移動found = false。這樣每次迭代都會重置爲false。現在,如果它被更改爲true,則對於該過程的其餘部分保持正確。

+0

非常感謝喲!對不起,延遲迴應! – mepclu 2012-07-24 15:54:57

+0

沒問題!我的回答並不像上述人那麼雄辯,所以我們需要小小的感謝!祝你好運! – Schwoebel 2012-07-26 20:05:25