简体   繁体   中英

Search string within a text file

I'm trying to search a text file for a set of keywords.

I put them as a string and I'm having a problem with the if statement.

On this line:

if(s.contains (keywords)) {

I didn't have it before. It says to do the following:

The method contains(CharSequence) in the type String is not applicable for the arguments (String[]))

But that just changes the String to CharSequence , still resulting in an error!

Here is the code:

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);
            }
        }
    }
}

Since String does not have the method String.contains(String) , you can achieve it as follows:

Change your keywords array to ArrayList<String> . Then as you read a line , get all the words in an array using String.split() method. Now you can loop through the word array ( created by reading the line from file) and check if the keywordList contains the word or not. Note: KeywordList.contains(s) will be true if s is exactly as a keyword. It will produce false if s is a string with other words but it contains one or more elements from keywords array .Thus this test will not produce an effective search result. The intention of the search is to check any input line s if it has at least one of the keywords from the keywords array. So one such solution can be as follows:

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;
                }
            }
        }
    }
}

A more efficient way will be to store the list of keywords in a Set and also the list of tokens on each line in a set. This prevents the inefficiency involved in iterating over all elements of the list. With that in mind, I've modified your code slightly:

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);
        }
    }
}

You cannot use a String array ( String[] ) with contains , use a regex instead, ie:

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);
    }

There is no such a method as contains(String[]). The correct one is contains(String)

like @Pedro Lobito said, you could use regex. Also you could loop through your string array perhaps,

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM