qq_34436013 2016-06-08 02:04 采纳率: 28.6%
浏览 1116
已采纳

java 求帮写一个关于读文件内容并计算打印的小程序

一列日,一列数值
要求如图,希望有空的前辈能花点时间帮忙写一下,非常感激!
图片说明
需要再增加一个功能,输入日期,就能显示对应日期的数字总合
图片说明

  • 写回答

3条回答 默认 最新

  • little_how 2016-06-08 03:46
    关注

    public static Map<String, Integer< countDate(String filePath) {
    Map<String, Integer< map = new HashMap<String, Integer<();
    FileInputStream in = null;
    try {
    //filepath为文件路径
    in = new FileInputStream(filePath);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while(true) {
    //按行读取
    String str = br.readLine();
    if (str == null) break;
    if ((str = str.trim()).length() == 0) continue;
    str = str.replaceAll("\s+", " ");
    String[] result = str.split(" ");
    Integer v = map.get(result[0]);
    v = v == null ? Integer.valueOf(result[1]) : v.intValue() + Integer.parseInt(result[1]);
    map.put(str, v);
    }
    Iterator<String> iterator = map.keySet().iterator();
    while(iterator.hasNext()) {
    String key = iterator.next();
    System.out.println("date="+key+",totalCount="+map.get(key));
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (in != null) {
    try {
    in.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    return map;
    }

    public static void read(String filePath) {
        Map&lt;String, Integer&lt;  map = countDate(filePath);
        System.out.print("请输入日期:");
        Scanner in = new Scanner(System.in);
        try {
            String date = in.nextLine();
            Integer count = map.get(date);
            int c = count == null ? 0 : count.intValue();
            System.out.println("该日期下的总数为:" + c);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
        把第一个方法改为有返回值的,然后main调用第二个方法就可以
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

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