user2917393
user2917393

Reputation: 85

Associating values with keys in a HashMap from text file [Java]

I have a text file that has a printer followed by a print job associated with that printer:

LaserJet This assignment involves . . .
LaserJet Budget for November . . .
Lexmark Invoice Number 334222: . . .
Samsung Class schedule for 2013/01/06 . . .
Lexmark Production schedule . . .
Samsung Class schedule for 2013/02/06 . . .
LaserJet Memo to Bobby Hill . . .
Lexmark Meeting in 20 minutes . . .
Lexmark Conference call transcript . . .
Lexmark Production schedule is off time . . .
Lexmark Last Production schedule . . .
LaserJet Print a crapload of pages . . .

I want to read the text file into my program and create a HashMap with the printer name as the key and associate the printjob with its corresponding printer. so i will end up with something like:

LaserJet
This assignment involves . . .
udget for November . . .
Memo to Bobby Hill . . .
Print a crapload of pages . . .

Samsung
Class schedule for 2013/01/06 . . .
Class schedule for 2013/02/06 . . .

Lexmark
Invoice Number 334222: . . .

etc. ( the file could contain 1000's of jobs with many different printers ). I am struggling with associating a printjob with its printer though. this is my code so far:

import java.util.*;
public class Printers {

private String sPrinterName;
private String sContent;
private Map<String, ArrayList<String>> mapOfJobs = new HashMap<String, ArrayList<String>>();
private ArrayList<String> collectionContent = new ArrayList<String>();
private ArrayList<String> collectionPrinters = new ArrayList<String>();

public Printers() { }

public void loadCollections( Scanner input ) {
    while ( input.hasNext() ) {
        sPrinterName = input.next(); // get printer name
        sContent = input.nextLine(); // get string ( print job )
        if ( !collectionPrinters.contains(sPrinterName))
            collectionPrinters.add(sPrinterName);
        collectionContent.add(this.sContent);
    }
}

public void buildMap() { // the process of creating the HashMap
    for ( int i = 0; i < collectionPrinters.size(); i++ ) {
        String refToPrinter = collectionPrinters.get(i);
        String refToContent = collectionContent.get(i);
        if ( !mapOfJobs.containsKey( refToPrinter ) ) {
                ArrayList<String> tmp = new ArrayList<String>();
                tmp.add( refToContent );
                mapOfJobs.put( refToPrinter, tmp );
        }
        else if ( !mapOfJobs.get( refToPrinter ).contains( refToContent ) )
            mapOfJobs.get( refToPrinter ).add( refToContent );
    }
} 

public void displayAll() { // print everything
    Iterator iterator = mapOfJobs.keySet().iterator();

    while (iterator.hasNext()) {
       String key = iterator.next().toString();
       String value = mapOfJobs.get(key).toString();

       System.out.println(key + " " + value);
    }

}

public String toString() {
    return String.format("Printer: %s     Job: %s", sPrinterName, sContent );
}
}

i have another class with main that creates a Printers object to call the methods. Anyway the code only outputs:

LaserJet [ This assignment involves . . .]
Lexmark [ Budget for November . . .]
Samsung [ Invoice Number 334222: . . .]

help is appreciated..

Upvotes: 1

Views: 1346

Answers (3)

Andreas Wederbrand
Andreas Wederbrand

Reputation: 39961

After your call to loadCollections the collections will have different number of rows. If the example text was the complete file you would have 3 rows in collectionPrinters and 12 rows in collectionContent. And you have removed the association between printers and content.

You just need to build your map, in pseudo code:

for each line
  if no map entry
    create map entry with content
  else
    append content to map entry

Upvotes: 1

Masudul
Masudul

Reputation: 21961

You don't need so many Collections, only mapOfJobs HashMap is enough,

 public void loadResult( Scanner input ) {
 while ( input.hasNextLine() ) {
    String line=input.nextLine();

    sPrinterName = line.subString(0, line.indexOf(' '));// printer name
    sContent = line.subString(line.indexOf(' ')+1);  // get string ( print job )

    List<String> contentList=mapOfJobs.get(sPrinterName);
    if(contentList==null ){
       contentList=new ArrayList<>();
     }
     contentList.add(sContent);
     mapOfJobs.put(sPrinterName,contentList)
  }
}

Upvotes: 1

A Paul
A Paul

Reputation: 8251

If I have understood properly, your issue is you want to read the jobs from a file and send that to a printer for printings.

Till now you are done with the Map creation and you are facing issue with the print job.

Please let me know if I have missed any point here.

I think while populating the HashMap you can use Map> so that you can maintain a queue for each printer. If you want the content of a printer to be unique i.e. same content can not be printed twice you can use Map>.

Not sure if it helps you. Let me know if you have any specific questions.

Upvotes: 1

Related Questions