我試圖讓程序工作,它運行和所有,但似乎相信每個字符串包含字符串數組中的單詞。我正在使用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;
}
}
}
對不起,我在這個網站上的第一個問題,所以如果我需要添加更多的信息只是問。 – derpaderp
一些示例輸入和預期輸出將會很有用 – alejandrogiron
我添加了輸入和預期輸出。 :D – derpaderp