Assistance appreciated. I create thread, I count occurrences and insert char with most occurrences into the map.
Not passing...
package com.codegym.task.task18.task1823;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*
Read file names from the console until the word "exit" is entered.
Pass the file name to the ReadThread thread.
The ReadThread thread should find the byte that occurs most frequently in the file, and add it to resultMap,
where the String parameter is the file name and the Integer parameter is the relevant byte.
Close the streams.
Requirements:
1. The program must read file names from the console until the word "exit" is entered.
2. For each file, create and start a ReadThread thread.
3. After each thread is start, ReadThread should create its own file input stream.
4. Then each thread must find the most frequently occurring byte in its file and add it to resultMap.
5. The file input stream in each thread must be closed.
*/
public class Solution {
public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fName = "";
while(!fName.equals("exit")){
fName = reader.readLine();
ReadThread readThr = new ReadThread(fName);
readThr.start();
}
reader.close();
}
public static class ReadThread extends Thread {
private String fileName;
public int i = Integer.MIN_VALUE;
public Character c = 0;
public ReadThread(String fileName) throws IOException {
this.fileName = fileName;
}
public void run(){
try {
FileInputStream fis = new FileInputStream(this.fileName);
TreeMap<Character, Integer> map = new TreeMap<>();
//see if exists in map, then up the count, else insert to map with 1 as count
while (fis.available() > 0){
map.merge((char) fis.read(), 1, Integer::sum);
}
fis.close();
//loop through treemap and find entry with highest int
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
if (entry.getValue()> i){
c = entry.getKey();
}
}
//add the character that occurred the most to the HashMap, as an integer
resultMap.put(this.fileName,Character.getNumericValue(c));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}