Skip to content

Instantly share code, notes, and snippets.

@dmersiyanov
Created June 16, 2017 10:45
Show Gist options
  • Save dmersiyanov/b15f2cea542e3b1f1d80d528552e423e to your computer and use it in GitHub Desktop.
Save dmersiyanov/b15f2cea542e3b1f1d80d528552e423e to your computer and use it in GitHub Desktop.
Решение задачи task1821 - Встречаемость символов. Программа запускается с одним параметром — именем файла, который содержит английский текст. Посчитать частоту встречания каждого символа. Отсортировать результат по возрастанию кода ASCII.
package com.javarush.task.task18.task1821;
/*
Встречаемость символов
*/
import java.io.FileInputStream;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
public static void main(String[] args) throws Exception {
String file = args[0];
FileInputStream fis = new FileInputStream(file);
TreeMap<Character, Integer> map = new TreeMap<>();
while (fis.available() > 0) {
char a = (char) fis.read();
if(!map.containsKey(a)) {
map.put(a, 1);
} else map.put(a, map.get(a)+1);
}
for(Map.Entry<Character, Integer> item : map.entrySet()){
System.out.println(item.getKey() + " " + item.getValue());
}
fis.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment