asked 17.3k views
4 votes
Suppose a library is processing an input file containing the titles of books in order to identify duplicates. Write a program that reads all of the titles from an input file called bookTitles.inp and writes them to an output file called duplicateTitles.out. When complete, the output file should contain all titles that are duplicated in the input file. Note that the duplicate titles should be written once, even though the input file may contain same titles multiple times. If there are not duplicate titles in the input file, the output file should be empty. Create the input file using Notepad or another text editor, with one title per line. Make sure you have a number of duplicates, including some with three or more copies.

Programming Language: Java

1 Answer

3 votes

Step-by-step explanation:

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.HashMap;

import java.util.Scanner;

public class DuplicateTitles {

public static void main(String[] args) {

// specify input and output file names

String inputFile = "bookTitles.inp";

String outputFile = "duplicateTitles.out";

// read book titles from input file and count occurrences of each title

HashMap<String, Integer> titleCounts = new HashMap<String, Integer>();

try {

Scanner scanner = new Scanner(new File(inputFile));

while (scanner.hasNextLine()) {

String title = scanner.nextLine();

if (titleCounts.containsKey(title)) {

titleCounts.put(title, titleCounts.get(title) + 1);

} else {

titleCounts.put(title, 1);

}

}

scanner.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

// write duplicate titles to output file

try {

PrintWriter writer = new PrintWriter(new File(outputFile));

for (String title : titleCounts.keySet()) {

if (titleCounts.get(title) > 1) {

writer.println(title);

}

}

writer.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

The program first declares the names of the input and output files. It then reads in the book titles from the input file using a Scanner object and stores them in a HashMap called titleCounts. For each title, the program checks if it has already been encountered and updates its count accordingly.

Once all titles have been read, the program writes the duplicate titles to the output file using a PrintWriter object. The program iterates through the titleCounts map, and for each title that has a count greater than one, it writes the title to the output file.

This program is efficient because it only stores one copy of each title in memory, regardless of how many times it appears in the input file. Additionally, it only writes duplicate titles once to the output file, as specified in the prompt.

answered
User Prasastoadi
by
7.6k points