Question

Java Exceptions Suppose a library is processing an input file containing the titles of books in...

Java Exceptions

Suppose a library is processing an input file containing the titles of books in order to remove 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 uniqueTitles.out. When complete, the output file should contain all unique titles found in the input file.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Implemented code as per the requirement. Please comment if you face any difficulty or if you need any modification.

Code:

=====

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

public class SodaCoke {

static File file1,file2;

static List lis;

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

file1 = new File("bookTitles.inp");

file2 = new File("uniqueTitles.out");

Scanner sc = new Scanner(file1);

Scanner sc1 = new Scanner(file1);

FileWriter fw = new FileWriter(file2);

lis = new ArrayList<String>();

String title=null,title1 = null;

int count = 0;

while(sc1.hasNext()){

title1 = sc1.nextLine();

if(!checkIfCompleted(title1)){

sc = new Scanner(file1);

while(sc.hasNext()){

title = sc.nextLine();

if(title1.equalsIgnoreCase(title)){

count++;

}

}

fw.write(title1 + "\t" + count+"\n");

lis.add(title1);

}

count=0;

}

fw.close();

}

private static boolean checkIfCompleted(String title) {

if(lis.contains(title)){

return true;

}

return false;

}

}

Ouput screen:

===========

2 berg 1 3 chris 2 4 kevin 2 maria 2 karr1 mark 7

Add a comment
Know the answer?
Add Answer to:
Java Exceptions Suppose a library is processing an input file containing the titles of books in...
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
  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

  • Implement the File Input/Output example presented in page 7. You need to create your own test.txt...

    Implement the File Input/Output example presented in page 7. You need to create your own test.txt file using any text editor. The input file should have some integer values (e.g., 10 values) separated by whitespaces. Normally this input file should be placed in the same directory as the program code resides. But depending on the IDE you use, it may be a different directory in the project. Figure out which directory your IDE reads an input file from and writes...

  • Java Programming Exercise: Creating and implementing a UML class diagram The inventory of a library is...

    Java Programming Exercise: Creating and implementing a UML class diagram The inventory of a library is a set of media. Media can be books, CDs and films. Each medium has a unique alphanumeric ID, a title, a year of publication and the date on which it was purchased. Each book also has an author and an ISBN. For each CD the artist and the number of titles are saved. Each movie has a genre and a format: either DVD or...

  • Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter...

    Create a class called DuplicateRemover. Create an instance method called remove that takes a single parameter called dataFile (representing the path to a text file) and uses a Set of Strings to eliminate duplicate words from dataFile. The unique words should be stored in an instance variable called uniqueWords. Create an instance method called write that takes a single parameter called outputFile (representing the path to a text file) and writes the words contained in uniqueWords to the file pointed...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • In Java. Write a program in a single file that: Main: Creates 10 random doubles, all...

    In Java. Write a program in a single file that: Main: Creates 10 random doubles, all between 1 and 11, Calls a class that writes 10 random doubles to a text file, one number per line. Calls a class that reads the text file and displays all the doubles and their sum accurate to two decimal places. There has to be three classes, main for create random numbers, one to write , one to read all in the same file...

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