package sk.upjs.cvicenie12;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class FrekvencnaAnalyza {
public Map<Character, Integer> vytvorTabulku(File subor) {
Map<Character, Integer> tabulka = new HashMap<>();
try (Scanner citac = new Scanner(subor)) {
while (citac.hasNext()) {
String slovo = citac.next();
for (int i = 0; i < slovo.length(); i++) {
char pismeno = slovo.charAt(i);
tabulka.put(pismeno, tabulka.getOrDefault(pismeno, 0) + 1);
}
}
} catch (FileNotFoundException e) {
System.err.println("Zlyhalo nacitanie.");
e.printStackTrace();
return null;
}
return tabulka;
}
public static void main(String[] args) {
FrekvencnaAnalyza analyzator = new FrekvencnaAnalyza();
Map<Character, Integer> tabulka = analyzator.vytvorTabulku(new File("subor.txt"));
for (int i = 0; i < 10; i++) {
if (tabulka.isEmpty()) {
break;
}
int maxVyskytov = Collections.max(tabulka.values());
int maxZnak = ' ';
for (char c : tabulka.keySet()) {
if (tabulka.get(c) == maxVyskytov) {
maxZnak = c;
break;
}
}
System.out.println(maxZnak + ":" + maxVyskytov);
tabulka.remove(maxZnak);
}
}
}