Advertisement
jaVer404

level18.lesson10.home06

Dec 17th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 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.         File file = new File(args[0]);
  25.         byte[]anotherOneBytesToDust = new byte[(int) file.length()];
  26.         FileInputStream fileInputStream = new FileInputStream(file);
  27.         fileInputStream.read(anotherOneBytesToDust);
  28.         fileInputStream.close();
  29.         for (int i = 0; i <= 127; i++) {
  30.             int conter = countChar(i,anotherOneBytesToDust);
  31.             if (conter > 0) {
  32.                 System.out.println((char)i + " " + conter);
  33.             }
  34.     }
  35.     }
  36.  
  37.     public static int countChar (int charCode, byte[]someArray)/*Не нужно тут выкидывать исключения*/
  38.     {
  39.         int counter = 0;
  40.         for (byte b : someArray) {
  41.             if ((char) charCode == (char)b) {
  42.                 counter++;
  43.             }
  44.         }
  45.         return  counter;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement