Dhairya
Dhairya

Reputation: 131

How to search string in a file and then search that string in another file

I'm trying to create a java program that can read a file named file1.txt and store its strings and search those strings to another file named file2.txt and if the match is not found then print that particular string from file1.txt.

public static void main(String[] args)
{
    try
    {
        BufferedReader word_list = new BufferedReader(new FileReader("file1.txt"));
        BufferedReader eng_dict = new BufferedReader(new FileReader("file2.txt"));

        String spelling_word = word_list.readLine();
        String eng_dict_word = eng_dict.readLine();

        while (spelling_word != null)
        {
            System.out.println(spelling_word);
            spelling_word = word_list.readLine();

            if(eng_dict_word.contains(spelling_word))
            {
                System.out.println("Word found "+spelling_word);
            }
            else
            {
                System.out.println("Word not found "+spelling_word);
            }
        }
        word_list.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

Right now I'm able to get data from file1.txt but unable to search file1's data for example to search word "Home" in file2.txt

See that here File1.txt contains Homee and File2.txt has Home, so Homee should be print

enter image description here

Upvotes: 2

Views: 184

Answers (2)

VSB
VSB

Reputation: 327

Use Regex for the specific need, below is the refactored code for your problem. Let me know if this works for you.

 public static void main(String[] args) throws IOException
 {

    try
    {
        BufferedReader word_list = new BufferedReader(new FileReader("resources/file1.txt"));
        BufferedReader eng_dict = new BufferedReader(new FileReader("resources/file2.txt"));

        String spelling_word = word_list.readLine();
        String eng_dict_word = eng_dict.readLine();

        int matchFound = 0;
        Matcher m = null;

        while (spelling_word != null)
        {
            // creating the pattern for checking for the exact match on file2.txt
            String spelling_word_pattern = "\\b" + spelling_word + "\\b";

            Pattern p = Pattern.compile(spelling_word_pattern);

            while(eng_dict_word !=null) {
                m = p.matcher(eng_dict_word);
                if(m.find()) {
                    matchFound = 1;
                    break;
                }
                eng_dict_word = eng_dict.readLine();
            }

            if(matchFound == 1) {
                System.out.println("Word found " + m.group());
            }else {
                System.out.println("Word not found "+ spelling_word);
            }

            spelling_word = word_list.readLine();
            eng_dict = new BufferedReader(new FileReader("resources/file2.txt"));
            eng_dict_word = eng_dict.readLine();
            matchFound = 0;
        }


        word_list.close();
        eng_dict.close();
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    }
}

file1.txt content

Homeee
Ho
hello
hom
xx
Me

file2.txt content

Home
Me
H
Homey

Result

Word not found Homeee
Word not found Ho
Word not found hello
Word not found hom
Word not found xx
Word found Me

Upvotes: 0

Lukas Novicky
Lukas Novicky

Reputation: 952

first, you need to read first file. Preferably to SET() as it will get rid of duplicate strings. You will have set1

When this is done, you need to read second file, and do the same. You will get set2

Now, you have need to use RemoveAll() method on set1 with set2 as parameter. What is remaining in set1 needs to be printed on scren. You can do it with lambda.

See THIS to get how to read file.

see code below:

    Set<String> set1 = new HashSet<>();
    Set<String> set2 = new HashSet<>();

    try (FileReader reader = new FileReader("file1.txt");
         BufferedReader br = new BufferedReader(reader)) {

        // read line by line
        String line;
        while ((line = br.readLine()) != null) {
            set1.add(line);
        }

    } catch (IOException e) {
        System.err.format("IOException: %s%n", e);
    }

    try (FileReader reader = new FileReader("file2.txt");
         BufferedReader br = new BufferedReader(reader)) {

        // read line by line
        String line;
        while ((line = br.readLine()) != null) {
            set2.add(line);
        }

    } catch (IOException e) {
        System.err.format("IOException: %s%n", e);
    }

    set1.removeAll(set2);
    set1.forEach(System.out::println);

Upvotes: 1

Related Questions