package com.codegym.task.task18.task1821;
import java.io.FileReader;
import java.io.IOException;
public class Solution {
public static void main(String[] args) throws IOException {
int[] aSCII = new int[128];
try (FileReader reader = new FileReader(args[0])) {
while(reader.ready()){
aSCII[reader.read()]++;
}
}
for (int i = 0; i < aSCII.length; i++) {
if (aSCII[i] != 0) {
System.out.println((char)i + " " + aSCII[i]);
}
}
}
}
I understand all in this code but I don'tk know why is correct? We not sort characerss and we don't calculate the frequencyWhy it is correct?
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
14 October 2020, 13:19
The array doesn't need to be sorted. Each index 0-127 is being used as a char and it is already in order. Line 11 reads 1 byte at a time that corresponds to a symbol on the ASCII table, it accesses the value at that index and increases it by one. This is counting each char.
+1