1

我想计算输入网址中的所有字母。我不想区分大写或小写字母。a的总量将存储为total[0]中的整数,b的总量为total[1]等。

知道如何使用 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;
    }

编辑:

感谢所有的回答者!你很棒!!;)

4

4 回答 4

3

不要使用Stream. 那些是用来阅读的byteReader如果需要字符,请使用 a 。byte可能适用于 ASCII,但字符最多可达 4 个字节,并且可能有不同的编码。

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;
}
于 2013-09-12T19:02:25.963 回答
1

你可以做这样的事情(伪代码):

int aCnt = totalInput.length() - totalInput.replaceIgnoreCase('a', '').length();
int bCnt = totalInput.length() - totalInput.replaceIgnoreCase('b', '').length();
于 2013-09-12T18:51:39.533 回答
1

这是使用地图的解决方案:

public static Map letterFrequency(String url) throws IOException {
    Map<Character, Integer> m = new HashMap<Character, Integer>();
    char[] urlCharArray = url.toCharArray(); 
    for (char a : urlCharArray) {
        Integer freq = m.get(a);
        m.put(a, (freq == null) ? 1 : freq + 1);
    }
    return m;
}
于 2013-09-12T19:01:58.313 回答
0

What's the encoding of the chars? not all encoding have 1 byte per character.

Assuming this will not be a problem, and by your first start of the question I want to count all the letters from an input url. just create a byte array for the 256 values of one byte, and count on it, like:

char[] b = new byte[256]; //one byte for each value
while (loop) {
    int r = inn.read();
    //verify for end-of-stream or other errors
    b[r]++;
}

this would give the char count for each value of the byte, like that:

b['a'] = a count
b['A'] = A count

now to turn to case insensitive

b['a'] + b['A']
于 2013-09-12T19:03:06.733 回答