Search code examples
javacharinputstreamcountingbufferedinputstream

How to count char with InputStream using java?


I want to count all the letters from an input url. I don't want to discriminate between uppercase or lowercase letters. The total amounts of a's will be stored as an integer in total[0], total amount of b's in total[1], etc. etc.

Any idea how I can achieve this using InputStream?

    public static int[] letterFrequency(String url) throws IOException {
        InputStream inn= new BufferedInputStream((new URL(url)).openStream());
        char[] c= {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'æ', 'ø', 'å'};
        int[] total= new int[29];

        for(int i= 0; i< c.length; i++)   {
            int counter= 0;
            while(inn.available()!= 0)  {
                if(inn.read()== c[i])
                    counter++;
            }

            total[i]= counter;
        }
        return total;
    }

EDIT:

Thank you for all the anwsers! You are great!! ;)


Solution

  • Don't use a Stream. Those are meant to read byte. Use a Reader if you want characters. byte might work for ASCII but characters can be up to 4 byte and may have different encodings.

    public static int[] letterFrequency(String url) throws IOException {
        Reader inn = new InputStreamReader(new BufferedInputStream((new URL(url)).openStream()), "UTF-8");
        char[] c = {
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
                'u', 'v', 'w', 'x', 'y', 'z', 'æ', 'ø', 'å'
        };
        String chars = new String(c);
    
        int[] total = new int[c.length];
        int read;
        while ((read = inn.read()) != -1) {
            read = Character.toLowerCase(read);
            int index = chars.indexOf(read);
            if (index != -1) {
                total[index]++;
            }
        }
        return total;
    }