Search code examples
javaarraylisthashmapfrequency

Incrementing HashMap values for key frequency doesn't print


I am currently working on a project to count the frequency of words in a text file. The driver program places the words into an ArrayList (after making them lowercase and removing whitespace), and then the FreqCount object places the ArrayList into a HashMap that will handle the frequency operations. So far, I can get the driver to read the text file, put it into an ArrayList, and then put that into the HashMap. My issue is that the HashMap nodes do not repeat, so I am trying to increment the value each time the word is seen.

Driver:

package threetenProg3;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;


public class Driver {
    
    public static void main(String[] args) throws FileNotFoundException{
        File in = new File("test.txt");
        Scanner scanFile = new Scanner(in);
        
        ArrayList<String> parsed = new ArrayList<String>();
        
        
        while(scanFile.hasNext()) { //if this ends up cutting off bottom line, make it a do while loop
            parsed.add(scanFile.next().toLowerCase());
        }
        
        for(int i = parsed.size()-1; i>=0; i--) { //prints arraylist backwards
            System.out.println(parsed.get(i));
        } //*/
        
        FreqCount fc = new FreqCount(parsed);
        
        System.out.println("\n Hashmap: \n");
        fc.printMap();
        
        scanFile.close();
        
    }
}

FreqCount:

package threetenProg3;

import java.util.HashMap;
import java.util.List;

public class FreqCount {
    //attributes and initializations
    private HashMap<String, Integer> map = new HashMap<String, Integer>();
    
    //constructors
    
    FreqCount(List<String> driverList){
        for(int dLIndex = driverList.size()-1; dLIndex>=0; dLIndex--) { //puts list into hashmap
            
            for(String mapKey : map.keySet()) {
                
                if(mapKey.equals(driverList.get(dLIndex))) {
                    int tval = map.get(mapKey);
                    
                    map.remove(mapKey);
                    
                    map.put(mapKey, tval+1);
                }else {
                    map.put(mapKey, 1);
                }
                
            }
                
        }
        
    }
    
    //methods
    
    public void printMap() {
        for (String i : map.keySet()) { //function ripped straight outta w3schools lol
              System.out.println("key: " + i + " value: " + map.get(i));
        }
    } //*/
    
}

Text file:

ONE TWO ThReE FoUR fIve
six     seven        
EIGHT
     NINE       
TEN ELEVEN
ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE ONE, ONE, ONE,

Output:

one,
one,
one,
one
one
one
one
one
one
one
one
one
one
one
one
one
one
eleven
ten
nine
eight
seven
six
five
four
three
two
one

 Hashmap: 

key: nine value: 1
key: one, value: 1
key: six value: 1
key: four value: 1
key: one value: 1
key: seven value: 1
key: eleven value: 1
key: ten value: 1
key: five value: 1
key: three value: 1
key: two value: 1
key: eight value: 1

From what I see, the output should be printing the correct values for the keys' frequencies. Thanks in advance for any help!


Solution

  • You can change the definition of FreqCount as follows:

    FreqCount(List<String> driverList) {
        for (int dLIndex = driverList.size() - 1; dLIndex >= 0; dLIndex--) {
            String key = driverList.get(dLIndex);
            if (map.get(key) == null) {
                map.put(key, 1);
            } else {
                map.put(key, map.get(key) + 1);
            }
        }
    }
    

    Output after this change:

     Hashmap: 
    
    key: nine value: 1
    key: one, value: 3
    key: six value: 1
    key: four value: 1
    key: one value: 15
    key: seven value: 1
    key: eleven value: 1
    key: ten value: 1
    key: five value: 1
    key: three value: 1
    key: two value: 1
    key: eight value: 1
    

    Alternatively,

    FreqCount(List<String> driverList) {
        for (int dLIndex = driverList.size() - 1; dLIndex >= 0; dLIndex--) {
            String key = driverList.get(dLIndex);
            map.put(key, map.getOrDefault(key, 0) + 1);
        }
    }
    

    Map#getOrDefault returns the value to which the specified key is mapped, or default value if this map contains no mapping for the key.