Question

In: Computer Science

JAVA PROGRAMMING LANGUAGE Suppose a library is processing an input file containing the titles of books...

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

Solutions

Expert Solution

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.


Related Solutions

Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as an int. How many times does the while loop execute, if the input is 38 20 4 0? Assume a Scanner object scnr has been instantiated, and that the tokens in the input are delimited by the space character. That means there are 4 tokens in the input. int num = 2000; while (num >= 20) { //Do something num = scnr.nextInt(); } select...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs in the data base are one-way. Question: Write a program that can answer if there is a path between any to vertices. For the vertex pairs use this as your input example: AL...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs are two-way. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of vertices traversed from X to Y if there is a path....
Language C: Suppose you are given a file containing a list of names and phone numbers...
Language C: Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
C Programming: Write a program that accepts 2 arguments, an input file and an output file....
C Programming: Write a program that accepts 2 arguments, an input file and an output file. The program is to store in the output file the contents of the input file in reverse. If the input file looks like this: Hello World.\n This is Line 2\n This is the end.\n then the output file should look like this: \n .dne eht si sihT\n 2 eniL si sihT\n .dlroW olleH The main program should look like this: int main(int argc, char...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT