2017-06-18 324 views
0

我試圖讓程序工作,它運行和所有,但似乎相信每個字符串包含字符串數組中的單詞。我正在使用openCSV庫嘗試通過包含同一列中的業務名稱和人員姓名的csv文件,並試圖使所有公司名稱都顯示爲第二列,並且所有人員姓名都會出現在第三欄。第一列只是一個識別號碼。如何正確地檢查一個字符串是否包含字符串數組中的另一個字符串? - JAVA

for (String[] row : inputEntries) 
    { 
     for(int i = 0; i < dictionary.length; i++) 
     { 
      String rowEntry = row[1].toLowerCase(); 
      String dictionaryTerm = dictionary[i].toLowerCase(); 

      if(rowEntry.contains(dictionaryTerm)) 
      { 
       String entries = row[0] + "," + row[1] + "," + ""; 
       String[] output = entries.split(","); 
       writer.writeNext(output); 
       System.out.println(output + ": This contained a Dictionary word"); 
       break; 
      } 
      else if (i == dictionary.length) 
      { 
       String entries = row[0] + "," + "" + "," + row[1]; 
       String[] output = entries.split(","); 
       writer.writeNext(output); 
       System.out.println(output + ": This did not contain a Dictionary word"); 
       break; 
      } 
     } 
    } 

它確實輸出並且打算在單獨的CSV,但它似乎認爲這一切都是一個企業的名字,所以我得到的是看起來與原來的文件。什麼是這種困境的可能解決方案?我濫用包含功能嗎?

輸入

"11111111","John Smith" 
"11111112","Wells Fargo Bank" 
"11111113","Company name LLC" 
"11111114","John Connor" 

輸出

"11111111","","John Smith" 
"11111112","Wells Fargo Bank",""  
"11111113","Company name LLC",""  
"11111114","","John Connor" 

所以它的工作多一點,我能得到它八九不離十做什麼,我想,但問題是,它似乎只被檢查字典字符串中的第一項。下面是更新後的代碼:

boolean match = false; 
    boolean nomatch = false; 
    int dicLength = dictionary.length; 

    for (String[] row : inputEntries) 
    { 
     for(int i = 0; i < dicLength; i++) 
     { 
      String rowEntry = row[1].toLowerCase(); 
      String dictionaryTerm = dictionary[i].toLowerCase(); 

      match = rowEntry.contains(dictionaryTerm); 

      if(match == true) 
      { 
       String entries = row[0] + "," + row[1] + "," + ""; 
       String[] output = entries.split(","); 
       writer.writeNext(output); 
       System.out.println(output + ": This contained a Dictionary word"); 
       match = false; 
       break; 
      } 

      if (i == (dicLength - 1)) 
      { 
       nomatch = true; 
      } 

      if (nomatch == true) 
      { 
       String entries = row[0] + "," + "" + "," + row[1]; 
       String[] output = entries.split(","); 
       writer.writeNext(output); 
       System.out.println(output + ": This did not contain a Dictionary word"); 
       match = false; 
       break; 
      } 
     } 
    } 
+0

對不起,我在這個網站上的第一個問題,所以如果我需要添加更多的信息只是問。 – derpaderp

+0

一些示例輸入和預期輸出將會很有用 – alejandrogiron

+0

我添加了輸入和預期輸出。 :D – derpaderp

回答

0

看你更新的代碼,你不小心設置匹配=假,而不是NOMATCH =假,當你檢查NOMATCH ==真。要解決這個問題的最好辦法是徹底和緊湊,排除了這些變量以下(也即破發,結束時不需要作爲循環即將關閉):

int dicLength = dictionary.length; 

    for (String[] row : inputEntries) { 
     for(int i = 0; i < dicLength; i++) { 

      String rowEntry = row[1].toLowerCase(); 
      String dictionaryTerm = dictionary[i].toLowerCase(); 

      if(rowEntry.contains(dictionaryTerm)) { 
       String entries = row[0] + "," + row[1] + "," + ""; 
       String[] output = entries.split(","); 
       writer.writeNext(output); 
       System.out.println(output + ": This contained a Dictionary word"); 
       break; 
      } 

      if (i == (dicLength - 1)) { 
       String entries = row[0] + "," + "" + "," + row[1]; 
       String[] output = entries.split(","); 
       writer.writeNext(output); 
       System.out.println(output + ": This did not contain a Dictionary word"); 
      } 
     } 
    } 

可以使這通過不檢查每次迭代的dic長度來提高效率(強制它在循環之外),但這取決於你! :)

+0

謝謝!!!這正是我需要的。它似乎正在做我所需要的事情。 – derpaderp

相關問題