Advertisement
jaVer404

level18.lesson10.home06 (not tested)

Dec 17th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package com.javarush.test.level18.lesson10.home06;
  2.  
  3. /* Встречаемость символов
  4. Программа запускается с одним параметром - именем файла, который содержит английский текст.
  5. Посчитать частоту встречания каждого символа.
  6. Отсортировать результат по возрастанию кода ASCII (почитать в инете). Пример: ','=44, 's'=115, 't'=116
  7. Вывести на консоль отсортированный результат:
  8. [символ1]  частота1
  9. [символ2]  частота2
  10. Закрыть потоки. Не использовать try-with-resources
  11.  
  12. Пример вывода:
  13. , 19
  14. - 7
  15. f 361
  16. */
  17.  
  18. import java.io.*;
  19.  
  20. public class Solution {
  21.     /*----------------------*/
  22.     public static void main(String[] args) throws IOException
  23.     {
  24.         byte[]anotherOneBytesToDust = fileToArray(args[0]);
  25.         for (int i = 0; i <= 127; i++) {
  26.             int conter = countChar(i,anotherOneBytesToDust);
  27.             if (conter> 0) {
  28.                 System.out.println((char)i + " " + conter);
  29.             }
  30.     }
  31.     }
  32.  
  33.     public static int countChar (int charCode, byte[]someArray) throws IOException
  34.     {
  35.         int counter = 0;
  36.         for (byte b : someArray) {
  37.             if ((char) charCode == (char)b) {
  38.                 counter++;
  39.             }
  40.         }
  41.         return  counter;
  42.     }
  43.  
  44.     public static byte[] fileToArray(String s) throws IOException
  45.     {
  46.             FileInputStream fileInputStream=null;
  47.             File file = new File(s);
  48.             byte[] bFile = new byte[(int) file.length()];
  49.             fileInputStream = new FileInputStream(file);
  50.             fileInputStream.read(bFile);
  51.             fileInputStream.close();
  52.             return bFile;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement