What kind of forbidden or dangerous operation am I using??...
And most importantly, what could be the basic bug in the program that is causing the test to fail?
(The previous problem has been fixed: this program no longer contains static variables.)
package com.codegym.task.task18.task1803;
import java.io.*;
import java.util.*;
/* Most frequent bytes */
public class Solution {
public static void main(String[] args) throws Exception {
Map<Integer, Integer> fileBytes = readFileToMap();
Integer highestValue = lookingForHighestValue(fileBytes);
ArrayList<Integer> resultList = lookingForRepeatingValue(fileBytes, highestValue);
printResults(resultList);
}
public static Map<Integer, Integer> readFileToMap() throws Exception {
BufferedReader inputConsole = new BufferedReader(new InputStreamReader(System.in));
String fileName = inputConsole.readLine();
inputConsole.close();
BufferedInputStream inputFromFile = new BufferedInputStream(new FileInputStream("/uploads/file.txt"));
Map<Integer, Integer> fileBytes = new HashMap<>();
while (inputFromFile.available()>0) {
Integer byteRead = inputFromFile.read();
if (fileBytes.containsKey(byteRead)) {
fileBytes.replace(byteRead, fileBytes.get(byteRead)+1);
}
else fileBytes.put(byteRead, 1);
}
inputFromFile.close();
return fileBytes;
}
public static Integer lookingForHighestValue(Map<Integer, Integer> fileBytes) {
Integer highestValue=0;
for (Integer val : fileBytes.values() ) {
if (val.compareTo(highestValue) > 0) {
highestValue = val;
}
}
return highestValue;
}
public static ArrayList<Integer> lookingForRepeatingValue(Map<Integer, Integer> fileBytes, Integer highestValue) {
ArrayList<Integer> resultList = new ArrayList<>();
for (Map.Entry<Integer, Integer> pair : fileBytes.entrySet() ) {
if (pair.getValue().equals(highestValue)) {
resultList.add(pair.getKey());
}
}
return resultList;
}
public static void printResults(ArrayList<Integer> resultList) {
int count = 0;
for( Integer textByte : resultList) {
count++;
System.out.print(textByte);
if (count<resultList.size()) System.out.print(" ");
}
}
}