I've added and removed the FOR loop printing the result to include/exclude an if statement.. neither pass even though the output is correct.
EG: Below vs what's in my solution.
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
if (entry.getValue() != 0) {
String str = String.valueOf(Character.toChars(entry.getKey()));
int freq = entry.getValue();
System.out.println(str + " " + freq);
}
}
I felt like creating a HashMap where key = ascii AND value = 0 was a good way to set the framework up for counting values.package com.codegym.task.task18.task1821;
/*
Symbol frequency
*/
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public static void main(String[] args) throws Exception {
String filename = args[0];
FileInputStream inputStream = new FileInputStream(filename);
HashMap<Integer,Integer> frequencyMap = new HashMap<Integer, Integer>();
for (int i = 0; i <= 255; i++) {
frequencyMap.put(i,0);
}
int key;
while (inputStream.available() > 0) {
key = inputStream.read();
frequencyMap.put(key, frequencyMap.get(key) + 1);
}
for (Map.Entry<Integer, Integer> result : frequencyMap.entrySet()) {
String str = String.valueOf(Character.toChars(result.getKey()));
int freq = result.getValue();
System.out.println(str + " " + freq);
}
inputStream.close();
}
}