Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level18.lesson10.home06;
- /* Встречаемость символов
- Программа запускается с одним параметром - именем файла, который содержит английский текст.
- Посчитать частоту встречания каждого символа.
- Отсортировать результат по возрастанию кода ASCII (почитать в инете). Пример: ','=44, 's'=115, 't'=116
- Вывести на консоль отсортированный результат:
- [символ1] частота1
- [символ2] частота2
- Закрыть потоки. Не использовать try-with-resources
- Пример вывода:
- , 19
- - 7
- f 361
- */
- import java.io.*;
- public class Solution {
- /*----------------------*/
- public static void main(String[] args) throws IOException
- {
- byte[]anotherOneBytesToDust = fileToArray(args[0]);
- for (int i = 0; i <= 127; i++) {
- int conter = countChar(i,anotherOneBytesToDust);
- if (conter> 0) {
- System.out.println((char)i + " " + conter);
- }
- }
- }
- public static int countChar (int charCode, byte[]someArray) throws IOException
- {
- int counter = 0;
- for (byte b : someArray) {
- if ((char) charCode == (char)b) {
- counter++;
- }
- }
- return counter;
- }
- public static byte[] fileToArray(String s) throws IOException
- {
- FileInputStream fileInputStream=null;
- File file = new File(s);
- byte[] bFile = new byte[(int) file.length()];
- fileInputStream = new FileInputStream(file);
- fileInputStream.read(bFile);
- fileInputStream.close();
- return bFile;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement