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 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分