2015-10-04 62 views
1

我正在嘗試搜索文本文件中的一組關鍵字。文本文件中的搜索字符串

我把它們作爲一個字符串,我遇到了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); 
      } 
     } 
    } 
} 
+0

你問了'String'包含'array',它不可能成功,主要是因爲沒有這樣的方法'包含(字符串[])'。相反,你需要另一個子循環,它循環每個keywoards並檢查它們是否存在於提供的String中 – MadProgrammer

回答

3

因爲字符串沒有方法String.contains(String),您可以按如下實現它:

關鍵字陣列更改ArrayList<String>。 然後當你讀一行時,使用String.split()方法獲取數組中的所有單詞。現在你可以遍歷數組(通過從文件中讀取行創建)並檢查關鍵字列表是否包含該詞。 注:如果s與關鍵字完全相同,則KeywordList.contains(s)將爲true。如果s是帶有其他詞的字符串,但它包含keywords數組中的一個或多個元素,則它將生成false。因此,此測試不會生成有效的搜索結果。搜索的目的是檢查任何輸入行s是否至少有一個keywords數組中的關鍵字。所以這樣的一個解決方案可以如下:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.ArrayList; 

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; 
     ArrayList<String> keywordList = new ArrayList<String>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:")); 

     String line; 
     while ((s = br.readLine()) != null) 
     { 
      String[] lineWordList = s.split(" "); 
      for (String word : lineWordList) 
      { 
       if (keywordList.contains(word)) 
       { 
        System.out.println(s); 
        String nextLine = br.readLine(); 
        System.out.println(nextLine); 
        break; 
       } 
      } 
     } 
    } 
} 
0

不能使用一個字符串數組(String[])與contains,使用正則表達式來代替,即:

import java.util.regex.*; 

    if (subjectString.matches("AB|BCD1|B11|BL:|B/1:|B1L:|B11:")) { 
     System.out.println(s); 
     String nextLine = br.readLine(); 
     System.out.println(nextLine); 
    } 
1

一種更有效的方式將存儲Set中的關鍵字列表以及集合中每行上的標記列表。這可以防止遍歷列表中的所有元素所帶來的低效率。考慮到這一點,我已經稍微修改了您的代碼:

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:"}; 
    Set<String> keywords = new HashSet<>(Arrays.asList("AB", "BC", "D1", "B11", "BL:", "B/1:", "B1L:", "B11:")); 
    String line; 
    Set<String> lineSet; 

    while ((s=br.readLine())!=null) { 
     lineSet = new HashSet(Arrays.asList(s.split(" "))); 
     if(!Collections.disjoint(lineSet, keywords)) { //returns true if both sets have no elements in common; 
      System.out.println(s); 
      String nextLine = br.readLine(); 
      System.out.println(nextLine); 
     } 
    } 
} 
0

沒有像contains(String [])這樣的方法。正確的是包含(字符串)

像@Pedro Lobito說的,你可以使用正則表達式。你也可以遍歷的字符串數組也許,

while ((s=br.readLine())!=null) { 
    for (String str: keywords) {   
     if(s.contains (str)) { 
      System.out.println(s); 
      String nextLine = br.readLine(); 
      System.out.println(nextLine); 
     } 
    } 
}