Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,376 questions

10,377 answers

573 users

How to create ASCII frequency table from a text file in Java

1 Answer

0 votes
// java code that create ASCII frequency table from a text file

package javaapplication1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class JavaApplication1 {
  
    public static final int ASCII = 128;

    public static void main(String[] args) {
        try
        {   
            int[] frequency = createTable("d:\\data.txt");


            if( frequency.length == 0)
                System.out.println("Error reading from file");
            else {
                for(int i = 32; i < ASCII; i++)
                    System.out.println("ASCII code: " + i + " ,character: " + (char)i 
                                                                            + " ,frequency: " 
                                                                            + frequency[i]);

                System.out.println("\nTotal characters in URL: " + sumTotal(frequency));
            }
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }

    private static int sumTotal(int[] array) {
        int total = 0;
        for(int x : array){
            total += x;
        }
        return total;
    }

    public static int[] createTable(String fileName) throws FileNotFoundException, IOException {
        int[] frequency = new int[ASCII];
        File f = new File(fileName);
        try (FileReader r = new FileReader(f)) {
            while(r.ready()) {
                int ch = r.read();
                if(ch >= 0 && ch < frequency.length)
                    frequency[ch]++;
                else
                    System.out.println("Not ASCII: " + ch + " " + (char)ch);
            }
        }
        return frequency;
    } 
}


/*
run:
 
ASCII code: 32 ,character:   ,frequency: 0
ASCII code: 33 ,character: ! ,frequency: 0
ASCII code: 34 ,character: " ,frequency: 0
ASCII code: 35 ,character: # ,frequency: 1
ASCII code: 36 ,character: $ ,frequency: 0
ASCII code: 37 ,character: % ,frequency: 0
ASCII code: 38 ,character: & ,frequency: 0
ASCII code: 39 ,character: ' ,frequency: 0
ASCII code: 40 ,character: ( ,frequency: 0
ASCII code: 41 ,character: ) ,frequency: 0
ASCII code: 42 ,character: * ,frequency: 0
ASCII code: 43 ,character: + ,frequency: 2
ASCII code: 44 ,character: , ,frequency: 0
ASCII code: 45 ,character: - ,frequency: 0
ASCII code: 46 ,character: . ,frequency: 0
ASCII code: 47 ,character: / ,frequency: 0
ASCII code: 48 ,character: 0 ,frequency: 0
ASCII code: 49 ,character: 1 ,frequency: 0
ASCII code: 50 ,character: 2 ,frequency: 0
ASCII code: 51 ,character: 3 ,frequency: 0
ASCII code: 52 ,character: 4 ,frequency: 0
ASCII code: 53 ,character: 5 ,frequency: 0
ASCII code: 54 ,character: 6 ,frequency: 0
ASCII code: 55 ,character: 7 ,frequency: 0
ASCII code: 56 ,character: 8 ,frequency: 0
ASCII code: 57 ,character: 9 ,frequency: 0
ASCII code: 58 ,character: : ,frequency: 0
ASCII code: 59 ,character: ; ,frequency: 0
ASCII code: 60 ,character: < ,frequency: 0
ASCII code: 61 ,character: = ,frequency: 0
ASCII code: 62 ,character: > ,frequency: 0
ASCII code: 63 ,character: ? ,frequency: 0
ASCII code: 64 ,character: @ ,frequency: 0
ASCII code: 65 ,character: A ,frequency: 0
ASCII code: 66 ,character: B ,frequency: 0
ASCII code: 67 ,character: C ,frequency: 0
ASCII code: 68 ,character: D ,frequency: 0
ASCII code: 69 ,character: E ,frequency: 0
ASCII code: 70 ,character: F ,frequency: 0
ASCII code: 71 ,character: G ,frequency: 0
ASCII code: 72 ,character: H ,frequency: 0
ASCII code: 73 ,character: I ,frequency: 0
ASCII code: 74 ,character: J ,frequency: 0
ASCII code: 75 ,character: K ,frequency: 0
ASCII code: 76 ,character: L ,frequency: 0
ASCII code: 77 ,character: M ,frequency: 0
ASCII code: 78 ,character: N ,frequency: 0
ASCII code: 79 ,character: O ,frequency: 0
ASCII code: 80 ,character: P ,frequency: 0
ASCII code: 81 ,character: Q ,frequency: 0
ASCII code: 82 ,character: R ,frequency: 0
ASCII code: 83 ,character: S ,frequency: 0
ASCII code: 84 ,character: T ,frequency: 0
ASCII code: 85 ,character: U ,frequency: 0
ASCII code: 86 ,character: V ,frequency: 0
ASCII code: 87 ,character: W ,frequency: 0
ASCII code: 88 ,character: X ,frequency: 0
ASCII code: 89 ,character: Y ,frequency: 0
ASCII code: 90 ,character: Z ,frequency: 0
ASCII code: 91 ,character: [ ,frequency: 0
ASCII code: 92 ,character: \ ,frequency: 0
ASCII code: 93 ,character: ] ,frequency: 0
ASCII code: 94 ,character: ^ ,frequency: 0
ASCII code: 95 ,character: _ ,frequency: 0
ASCII code: 96 ,character: ` ,frequency: 0
ASCII code: 97 ,character: a ,frequency: 2
ASCII code: 98 ,character: b ,frequency: 0
ASCII code: 99 ,character: c ,frequency: 3
ASCII code: 100 ,character: d ,frequency: 0
ASCII code: 101 ,character: e ,frequency: 0
ASCII code: 102 ,character: f ,frequency: 0
ASCII code: 103 ,character: g ,frequency: 0
ASCII code: 104 ,character: h ,frequency: 2
ASCII code: 105 ,character: i ,frequency: 0
ASCII code: 106 ,character: j ,frequency: 1
ASCII code: 107 ,character: k ,frequency: 0
ASCII code: 108 ,character: l ,frequency: 0
ASCII code: 109 ,character: m ,frequency: 0
ASCII code: 110 ,character: n ,frequency: 1
ASCII code: 111 ,character: o ,frequency: 1
ASCII code: 112 ,character: p ,frequency: 3
ASCII code: 113 ,character: q ,frequency: 0
ASCII code: 114 ,character: r ,frequency: 0
ASCII code: 115 ,character: s ,frequency: 0
ASCII code: 116 ,character: t ,frequency: 1
ASCII code: 117 ,character: u ,frequency: 0
ASCII code: 118 ,character: v ,frequency: 1
ASCII code: 119 ,character: w ,frequency: 0
ASCII code: 120 ,character: x ,frequency: 0
ASCII code: 121 ,character: y ,frequency: 1
ASCII code: 122 ,character: z ,frequency: 0
ASCII code: 123 ,character: { ,frequency: 0
ASCII code: 124 ,character: | ,frequency: 0
ASCII code: 125 ,character: } ,frequency: 0
ASCII code: 126 ,character: ~ ,frequency: 0
ASCII code: 127 ,character:  ,frequency: 0

Total characters in URL: 29

*/

 




answered Jun 30, 2017 by avibootz
...