/****************************************************************************** * This program reads bytes from STDIN and then computes a frequency table. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import org.pureprogrammer.Utils; public class FrequencyTableBytes { public static void main(String[] args) { int totalCount = 0; List countTable = new ArrayList<>(Collections.nCopies(256, null)); for (int x = 0; x <= 255; ++x) { countTable.set(x, 0); } int c; while ((c = Utils.getbyte(System.in)) != -1) { countTable.set(c, countTable[c] + 1); ++totalCount; } System.out.println("Dec\tHex\tCount\tFreq"); for (int x = 0; x <= 255; ++x) { if (countTable.get(x) > 0) { final double FREQ = countTable.get(x) / (double)(totalCount); System.out.println(Utils.format("{0:d}\t{0:02x}\t{1:d}\t{2:.4f}", x, countTable.get(x), FREQ)); } } } }