In: Computer Science
JAVA PROGRAMMING LANGUAGE
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.
PLEASE WRITE A PROGRAM WITHOUT ANY ACCESS ERROR OR EXPLAIN HOW TO
AVOID IT
I have implemented a java program which fetch book titles from the bookTitles.txt file then it will be stored into the HashSet and store the duplicate value using the arrayList and then store all the duplicate values to the duplicateTitles.txt file.
Here, I used HashSet which is used to store only single copy value. I does not store duplicate values.
How to fetch duplicate values using HashSet and ArrayList.
STEP 1: We store the values by checking it is not in the HashSet using the hashSet.contains() method which returns true if value is already into the hash set.
STEP 2: Then after which values that are already inserted into the hash set that are never stored into the hash set so that values we will store to the array list and we will get the duplicate Book Titles from the "bookTitles.txt" file.
Program:-
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
public class GenerateDuplicateBookTitle {
public static void main(String[] args) {
// declare and intialize path whereyour input file is stored
String filePath = "D:\\";
// intialize input file name and output file name
String inputFile = "bookTitles.txt";
String ouputFile = "duplicateTitles.txt";
// create HashSet which does not store duplicte values
HashSet<String> bookTitles = new HashSet<>();
// create arrayList which stores only duplicate bok titles
ArrayList<String> duplicateBookTitles = new ArrayList<>();
// now read the book titles from the bookTitles.txt
try{
// create an object Of fileReader class with the specified filename with the path
FileReader fr = new FileReader(filePath+inputFile);
// create an object of BufferedReader class for reading line from txt file
BufferedReader br = new BufferedReader(fr);
String getLine = "";
System.out.println("-------------- Fetch data from the file ---------------\n");
while((getLine = br.readLine()) != null){
// add book title to the bookTitles arrayList
if(!bookTitles.contains(getLine)){
// diplay to the console
System.out.println(getLine+" readed successfully from "+filePath+inputFile);
// add to the hash set
bookTitles.add(getLine);
}else{
// when HashSet does not store duplicate value then this vlue we store into the duplicateBookTitles
duplicateBookTitles.add(getLine);
}
}
// display duplicate book title into the console
System.out.print("Duplicate book titles fetched from "+filePath+inputFile+" : ");
System.out.println(duplicateBookTitles.toString());
// now store it into the "duplicateTitles.txt" file
// create an object of FIleWriter class for writing data into the txt file
FileWriter write = new FileWriter(filePath+ouputFile);
System.out.println("\n------------ Write Duplicate BookTitles ----------------\n");
// now get each element from the duplicateBookTitles arrayList
for(String duplicateBookTitle : duplicateBookTitles){
// write into the "duplicateTitles.txt" file
write.write(duplicateBookTitle+"\n");
// print on console
System.out.println(duplicateBookTitle+" writed succssfully into the "+filePath+ouputFile);
}
// close the writer
write.close();
fr.close();
br.close();
}catch(FileNotFoundException e){
System.out.println("FILE '"+inputFile+"' IS NOT FOUND in "+filePath);
} catch (IOException ex) {
System.out.println(ex);
}
}
}
Output:-
1> input file (bookTitles.txt):-
2> console output of program
3> output file (duplicateTitles.txt):-
I hope you will understand how to fetch duplicate values using HashsSet and ArrayList.
Do you feel needful and useful then please upvote me.
Thank you.