1

入力 URL からすべての文字をカウントしたい。大文字と小文字を区別したくありません。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

1

次のようなことができます (疑似コード):

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