Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.nsu.ccfit.matveev;
- import java.util.*;
- import java.util.Map.Entry;
- import java.lang.StringBuilder;
- import java.io.*;
- public class lab1 {
- private static void getInfo(String file) {
- Reader reader = null;
- HashMap<String, Integer> hm = new HashMap<String, Integer>();
- try {
- long count = 0;
- reader = new InputStreamReader(new FileInputStream(file));
- while (reader.ready()) {
- char ch;
- StringBuilder sb = new StringBuilder();
- while (Character.isLetterOrDigit(ch = (char) reader.read()))
- sb.append(ch);
- if (0 == sb.length())
- continue;
- count++;
- String str = sb.toString();
- if (hm.containsKey(str)) {
- int value = (Integer) hm.get(sb.toString()) + 1;
- hm.put(str, value);
- } else
- hm.put(str, 1);
- }
- List<Entry<String, Integer>> s = new ArrayList<Entry<String, Integer>>(
- hm.entrySet());
- Collections.sort(s, new Comparator<Entry<String, Integer>>() {
- public int compare(Entry<String, Integer> o1,
- Entry<String, Integer> o2) {
- return -o1.getValue().compareTo(o2.getValue());
- }
- });
- for(Entry<String, Integer> e: s){
- double percent = ((Integer) e.getValue()).doubleValue();
- System.out.printf("%s;%d;%3.2f%%\n", e.getKey(),
- e.getValue(), percent / count * 100);
- }
- } catch (IOException e) {
- System.err.println("Error while reading file: "
- + e.getLocalizedMessage());
- } finally {
- if (null != reader) {
- try {
- reader.close();
- } catch (final IOException e) {
- e.printStackTrace(System.err);
- }
- }
- }
- }
- public static void main(String[] args) {
- String file = args[0];
- getInfo(file);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement