I have begun to feel that the instructions for these task are becoming very vague, but a very specific solution is required to pass. I had my code outputting the result set added to the resultMap hash map, and the results added there were correct for the input files I used, yet it still doesn't verify. I've tried a lot of minor modifications at this point and I'm very frustrated because I don't know what the checker wants. Can someone that managed to solve this please look at my code and offer advice? Thank you all so much in advance!
Jeremy
package com.codegym.task.task18.task1823;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
/*
Threads and bytes
*/
public class Solution {
public static volatile Map<String, Integer> resultMap = new HashMap<String, Integer>();
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filename = null;
ReadThread rd;
while (true) {
try {
filename = reader.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
if (filename.equals("exit")) {
break;
} else {
rd = new ReadThread(filename);
rd.start();
try {
rd.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
try {
reader.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static class ReadThread extends Thread {
String filename;
int data;
HashMap<Integer, Integer> hashMap = new HashMap<>();
FileInputStream fis;
String strData;
int max = 0;
public ReadThread(String fileName) {
// Implement constructor body
this.filename = fileName;
}
// Implement file reading here
public void run() {
try {
fis = new FileInputStream(filename);
while (fis.available() > 0) {
data = fis.read();
if (!hashMap.containsKey(data)) {
hashMap.put(data, 1);
if (max < 1) {
max = 1;
}
}
else {
hashMap.put(data, hashMap.get(data) + 1);
if ((int) hashMap.get(data) > max) {
max = hashMap.get(data);
}
}
}
for (Map.Entry e : hashMap.entrySet()) {
if ((int) e.getValue() == max) {
synchronized (resultMap) {
resultMap.put(Integer.toString((int) e.getKey()), (Integer) e.getValue());
}
}
}
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}