-4

I have an ArrayList<String> that contains 3 words ("one", "two" and "three"). There is a file of strings. I need to find strings in the file that contain exactly two items in the ArrayList. How can I do this?

bcsb1001
  • 2,606
  • 3
  • 22
  • 33
nesher
  • 13
  • 2
  • 5
    Thanks for sharing that with us. If you have a specific problem during your implementation, then include that information and we'll help you solve it. Good luck with your program! – Zircon Jun 16 '16 at 18:38
  • You can use java Scanner for reading the file and for each string you can run a loop and check your condition.[http://stackoverflow.com/questions/13185727/reading-a-txt-file-using-scanner-class-in-java] – GOXR3PLUS Jun 16 '16 at 18:54
  • The code looks like this: – nesher Jun 16 '16 at 19:18
  • Due to conditions I have to read lines this way: BufferedReader fileReader = new BufferedReader(new FileReader(fileName)); – nesher Jun 16 '16 at 19:24
  • And then by using filereader.readline() – nesher Jun 16 '16 at 19:25

1 Answers1

-1

First you have to read the string/content which should be analyzed. Then you can count keywords from your ArrayList in the content. For each keyword you test if it's in the content, in this case the count is increased. If the count is 2 you found your string.

public ArrayList<String> findStrings(ArrayList<String> searchStrings) {
    ArrayList<String> foundStrings = new ArrayList<>();
    while (hasMoreLines()) {
        String content = getNextString();
        if (countStrings(content, searchStrings) == 2) {
            foundStrings.add(content);
        }
    }
    return foundStrings;
}

private int countStrings(String content, ArrayList<String> searchStrings) {
    int count = 0;
    for (String searchString : searchStrings) {
        if (content.contains(searchString)) {
            count++;
        }
    }
    return count;
}

hasMoreLines and getNextString are missing and must be implemented, perferable with BufferedReader.