【问题标题】:How to count char with InputStream using java?java - 如何使用Java计算带有InputStream的字符?
【发布时间】:2013-09-17 07:13:51
【问题描述】:

我想计算输入网址中的所有字母。我不想区分大写或小写字母。 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;
    }

编辑:

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

【问题讨论】:

  • 首先你为什么要从 InputStream 中准备好字符?不是已经有 inputstreamReader 类了吗?
  • 您可以使用Map 并将每个计数器存储在其中,并将字母的小写/大写作为键。
  • 使用MaptoLowerCase 方法忽略大小写。你的方法行不通……

标签: java char inputstream counting bufferedinputstream


【解决方案1】:

不要使用Stream。这些是为了阅读byte。如果需要字符,请使用 Readerbyte 可能适用于 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;
}

【讨论】:

    【解决方案2】:

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

    int aCnt = totalInput.length() - totalInput.replaceIgnoreCase('a', '').length();
    int bCnt = totalInput.length() - totalInput.replaceIgnoreCase('b', '').length();
    

    【讨论】:

      【解决方案3】:

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

      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;
      }
      

      【讨论】:

        【解决方案4】:

        字符的编码是什么?并非所有编码每个字符都有 1 个字节。

        假设这不是问题,并且在您第一次开始问题时我想计算输入网址中的所有字母。只需为一个字节的 256 个值创建一个字节数组,并依靠它,例如:

        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]++;
        }
        

        这将给出每个字节值的字符数,如下所示:

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

        现在转向不区分大小写

        b['a'] + b['A']
        

        【讨论】:

          猜你喜欢
          • 2016-06-13
          • 1970-01-01
          • 1970-01-01
          • 2011-03-03
          • 1970-01-01
          • 1970-01-01
          • 2014-04-02
          • 2011-03-03
          • 1970-01-01
          相关资源
          最近更新 更多