Kommander Kitten
Kommander Kitten

Reputation: 283

Printing a HashMap into a .txt file

Right now, I'm trying to get this method of mine to print a .txt file of a HashMap that contains a word as a Key, and the number of times it appears in a read .txt file (done in another method) as a Value. The method needs to put the HashMap Keys in alphabetical order, and then print the corresponding Value next to it in a separate .txt file.

Here is my code for the method:

  public static void writeVocabulary(HashMap<String, Integer> vocab, String fileName) {

    // Converts the given HashMap keys (the words) into a List.
    // Collections.sort() will sort the List of HashMap keys into alphabetical order.
    List<String> listVal = new ArrayList<String>(vocab.keySet()); 
    Collections.sort(listVal);


    try 
    {
      // Creating the writer
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName))); 

      for (int i = 1; i < listVal.size(); i++) {
        out.println(listVal.get(i) + " " + vocab.get(i));
      }

      out.close();
    }
    // Catching the file not found error
    // and any other errors
    catch (FileNotFoundException e) {
      System.err.println(fileName + "cannot be found.");
    }
    catch (Exception e) {
      System.err.println(e);
    }
  }

My problem is that, while a .txt file is printed, and the words are in perfect ASCII order (what I need), every value next to the word is returned null. I've tried many different ways of fixing this but to no avail. I think the problem is in my 'for' loop:

   for (int i = 1; i < listVal.size(); i++) {
    out.println(listVal.get(i) + " " + vocab.get(i));
      }

I'm pretty sure my logic in this is faulty but I can't think of a solution. Any help would be much appreciated. Thanks in advance!

Upvotes: 0

Views: 3029

Answers (3)

MT0
MT0

Reputation: 167962

You don't need to iterate through the list using the indexes; instead you can use:

for ( final String key : listVal ) {
    out.println( key + " " + vocab.get( key ) );
}

and you can simplify things even further using a TreeSet to do the sorting:

for ( final String key : new TreeSet<String>( vocab.keySet() ) ) {
    out.println( key + " " + vocab.get( key ) );
}

Upvotes: 1

RealSkeptic
RealSkeptic

Reputation: 34618

This is where an enhanced for loop would have kept you from making a mistake. You get values from a Map by using get(key):

for ( String key : listVal ) {
    out.println( key + " " + vocab.get(key) );
}

Upvotes: 1

copeg
copeg

Reputation: 8348

You need to use the correct map key to get the value from the map - this code currently uses the index in the List, not the value in the list (which is the actual key to the map).

for (int i = 0; i < listVal.size(); i++) {
    out.println(listVal.get(i) + " " + vocab.get(listVal.get(i)));
}

And also start at index 0 if you want all the items (see initial condition in loop above). As suggested in a comment, you can alternatively use a TreeMap to iterate over the keys of the map in order

Upvotes: 3

Related Questions