qq_35516286 2018-09-25 03:23 采纳率: 100%
浏览 1393
已采纳

Java 统计字符串的长度,忽略字母的大小写

You will write an Average Word Length program that uses a Boolean parameter called caseSensitive to determine whether the Mapper class should treat upper and lower case letters as different (case-sensitive) or whether all letters should be converted to lower case (case-insensitive).
Write down the Mapper output, Reducer input, and Reducer output using the case- insensitive version of your program for the test input provided above.

  • 写回答

3条回答 默认 最新

  • threenewbee 2018-09-25 03:49
    关注
     import java.util.HashMap;
    import java.util.Set;
    class Untitled {
        public static double foo(String s, Boolean caseSensitive)
        {
            HashMap map = new HashMap();
            String[] words = s.split("[\\,\\(\\)\\.\\s\\r\\n\\-]");
            for (String item : words)
            {
                if (item.trim().equals("")) continue;
                if (!caseSensitive) item = item.toLowerCase();
                if (!map.containsKey(item)) map.put(item, item.length());
            }
            double r = 0.0;
            Set<String> wl = map.keySet();
            for (String w : wl) r += (double)w.length();
            r /= (double)wl.size();
            return r;
        }
    
        public static void main(String[] args) {
            String s = "You will write an Average Word Length program that uses a Boolean parameter called caseSensitive to determine whether the Mapper class should treat upper and lower case letters as different (case-sensitive) or whether all letters should be converted to lower case (case-insensitive).\r\nWrite down the Mapper output, Reducer input, and Reducer output using the case- insensitive version of your program for the test input provided above.";     
            System.out.println(foo(s, true));
            System.out.println(foo(s, false));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器