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,369 questions

10,368 answers

573 users

How to create ASCII frequency table from a web page (url) in Java

1 Answer

0 votes
// java code that create ASCII frequency table from a web page (url)

package javaapplication1;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

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

    public static void main(String[] args) {
        try
        {   
            int[] frequency = createFrequencyTableURL("http://www.collectivesolver.com");

            if( frequency.length == 0)
                System.out.println("Error reading from url");
            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[] createFrequencyTableURL (String url) {
        if(url == null)
            throw new IllegalArgumentException("url is null");

        int[] frequency = new int[ASCII];
        try {
            URL inputURL = new URL(url);
            try (InputStreamReader in = new InputStreamReader(inputURL.openStream())) {
                while(in.ready()){
                    int ch = in.read();
                    if(ch >= 0 && ch < frequency.length)
                        frequency[ch]++;
                    else
                        System.out.println("Not ASCII: " + ch + " " + (char)ch);
                }   
            }    
        }
        catch(MalformedURLException e){
            System.out.println(e);
            frequency = new int[0];
        }
        catch(IOException e){
            System.out.println(e);
            frequency = new int[0];
        }
        return frequency;
    }
}


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

Total characters in URL: 14026

*/

 




answered Jun 30, 2017 by avibootz
edited Jun 30, 2017 by avibootz
...