我正在嘗試搜索文本文件中的一組關鍵字。文本文件中的搜索字符串
我把它們作爲一個字符串,我遇到了if
聲明的問題。
在此行中:
if(s.contains (keywords)) {
我以前沒有它。它說要做到以下幾點:
的方法包括(CharSequence中)在String類型是不適用的參數(字符串[]))
但是,這只是改變的字符串CharSequence
,仍然導致錯誤!
下面是代碼:
import java.io.*;
public class SearchTextFile {
public static void main(String args[]) throws Exception {
int tokencount;
FileReader fr = new FileReader("c:\\searchtxt.txt");
BufferedReader br = new BufferedReader(fr);
String s;
int linecount = 0;
String[] keywords = {
"AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:"
};
String line;
while ((s = br.readLine()) != null) {
if (s.contains(keywords)) {
System.out.println(s);
String nextLine = br.readLine();
System.out.println(nextLine);
}
}
}
}
你問了'String'包含'array',它不可能成功,主要是因爲沒有這樣的方法'包含(字符串[])'。相反,你需要另一個子循環,它循環每個keywoards並檢查它們是否存在於提供的String中 – MadProgrammer