Question

2. Write a program to read two lists of names from two input files and then...

2. Write a program to read two lists of names from two input files and then match the names in the two lists using Co-sequential Match based on a single loop. Output the names common to both the lists to an output file,

In Java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question, When you run the program please ensure you update the input file names and output file name in the commented section below


=======================================================================


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class CoSequentialMatch {

    public static void main(String[] args) {
        // first file path and name
        String firstFile = "names.txt";
        // second file path and name
        String secondFile = "names_ (2).txt";
        ArrayList<String> firstFileNames = getNamesFromFile(firstFile);
        ArrayList<String> secondFileNames = getNamesFromFile(secondFile);

        int minSize = firstFileNames.size() < secondFileNames.size() ?
                firstFileNames.size() : secondFileNames.size();

        // output file path and name
        String outputFile = "common-names.txt";
        try {
            FileWriter writer = new FileWriter(new File(outputFile));
            for (int i = 0; i < minSize; i++) {
                if (firstFileNames.get(i).equalsIgnoreCase(secondFileNames.get(i))) {
                    writer.write(firstFileNames.get(i) + "\r\n");
                }
            }
            writer.close();
            System.out.println(outputFile+" generated successfully.");
        } catch (IOException e) {
            System.out.println("Error: Uanble to open/write data to file: " + outputFile);
            System.exit(1);
        }
    }

    private static ArrayList<String> getNamesFromFile(String fileName) {

        ArrayList<String> data = new ArrayList<>();
        try {
            Scanner scanner = new Scanner(new File(fileName));
            while (scanner.hasNextLine()) {
                data.add(scanner.nextLine());
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            System.out.println("Error: Unable to find/read file: " + fileName);
            System.exit(1);

        }
        return data;
    }
}

===============================================================

Add a comment
Know the answer?
Add Answer to:
2. Write a program to read two lists of names from two input files and then...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT