You have been asked to develop an application that will read book information from a text file to determine which publishers you use. The application will print a report of all unique publishers used.
Johnny Smith xxxxxxxxxx Harper Collins
Freedie Jones xxxxxxxxxx Harper Collins
Lilly Laine xxxxxxxx Penquin
Bobby Crosby xxxxxxx Atlantic
Troy James xxxxxxxx Atlantic
Output:
Atlantic
Penquin
HarperCollins

Question: Using the TreeSet class, how do I accomplish this, we just started and not sure of how to approach, orginally I developed with the ArrayList, but Prof wants us to use the TreeSet class. 5 1/2 weeks not enough time to absorb it all. Thanks in advance....destined2bbless!:S If I have posted this wrong please instruct on the correct way....thanks

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Publishers;

/**
 *
 * @author xxxx
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.IllegalFormatException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Set;

public class ProcessPublisher
{
    private static Scanner inputPublisherRecord;
    private static FileWriter outputPublisherList;
    private static PublisherRecord publisherRecord;
    private ArrayList publisherList = new ArrayList();
    private TreeSet<String> publisherSet = new TreeSet <String>();

    public void openFiles()
    {
        try
        {
            inputPublisherRecord = new Scanner(new File("BookRecords.txt"));
            outputPublisherList = new FileWriter("PublisherList.txt", true);
        }
        catch(FileNotFoundException fileNotFoundException)
        {
            System.err.println("File not found, could not be open.");
            System.exit(1);;
        }
        catch(SecurityException securityException)
        {
            System.err.println("Restricted file access, cannot be opened.");
            System.exit(1);
        }
        catch(IOException ioException)
        {
            Logger.getLogger(ProcessPublisher.class.getName()).log(Level.SEVERE, null
                    ,ioException);
            System.exit(1);
        }
    }
    public PublisherRecord getPublisherRecord()
    {
        PublisherRecord record = new PublisherRecord();
        if(inputPublisherRecord.hasNext())
        {
            record.setFirstName(inputPublisherRecord.next());
            record.setLastName(inputPublisherRecord.next());
            record.setISBN(inputPublisherRecord.nextInt());
            record.setPublisherName(inputPublisherRecord.next());
        }
    
    else
    {
        record = null;
    }
    return record;
    }
    public void processBookRecord()
    {
        publisherRecord = getPublisherRecord();
        {
            try
            {
                while(publisherRecord != null)
                {
                   if(!publisherSet.contains(publisherRecord.getPublisherName()))
                   {
                       this.publisherSet.add(publisherRecord.getPublisherName());
                   }
                   {
                       Set<String> set = new TreeSet<String>();

                      //publisherList.add(publisherRecord.getPublisherName());
                      //StringBuffer tempRecord = new StringBuffer();
                      //tempRecord.append(publisherRecord.getPublisherName());
                      //tempRecord.append(System.getProperty("line.separator"));
                      //outputPublisherList.write(tempRecord.toString().toCharArray());
                   }
                }
            }
            catch(IllegalFormatException formatException)
            {
                System.err.println("There is an error with the output.");
                System.exit(1);
            }
            catch(IOException ioException)
            {
                System.err.println("Error creating the file.");
                System.exit(1);
            }
        }

    }
    public void closeFiles()
    {
        try
        {
            if(inputPublisherRecord !=null)
            {
                inputPublisherRecord.close();
            }
            if(outputPublisherList !=null)
            {
                outputPublisherList.close();
            }
        }
        catch(IOException ioException)
        {
            System.err.println("Error closing file(s)");
            System.exit(1);
        }
    }

}

> Using the TreeSet class, how do I accomplish this

It's pretty simple; just keep on reading the lines from the text file and add the last entry i.e. the publisher name to a TreeSet<String> . So you'd probably have a `parse' method which would take a File instance [the handle to the data file] and a method which returns the list of all publisher names in the TreeSet. Of course, to make it more *OO*, you need to put in a bit more effort when structuring your classes.

BTW, package names should always contain only lowercase characters. Don't catch exceptions if you are anyways not going to do anything specific with them. Try to break processing logic and the data concerning the logic into separate classes rather than having a single class with lot of static members.

Thanks for the tips, as far as the classes are concern, this is the requirement set before us, anything else and points are deducted. :)

How do I do that....I'm very new to Java....would it be "set" or "tree" to read the file and print to screen?

> How do I do that....I'm very new to Java

Steps:

  • Declare a TreeSet<String> instance variable for your class
  • Read the file using Scanner
  • For each line read, create the PublisherRecord instance as per your requirement
  • Push or add the publisher name to the TreeSet
  • After the file parsing is complete, your TreeSet will contain the list of unique publisher names
  • Write a separate method to print the contents of the TreeSet as desired

> 5 1/2 weeks not enough time to absorb it all.

That should be more than enough IMO given that reading the documentation of the TreeSet class and absorbing the examples doesn't take more than a day even if you are new to Java programming [and not programming in general].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.