在Java中映射Reduce

时间:2017-04-15 13:27:20

标签: java

所以我需要完成以下任务: 游戏公司在每小时结束时从其玩家接收数据。每当玩家在游戏中购买某物时,就会在CSV文件中生成一个新行,格式如下:

Input:
fileContent: "2017-04-01, 11:25, 2; 2017-04-02, 11:30, 8; 2017-04-03, 14:42, 9; 2017-04-01, 11:42, 700; 2017-04-01, 11:00, 25; 2017-04-02, 14:56, 23; 2017-04-02, 18:20, 57; 2017-04-03, 14:30, 8; 2017-04-03, 14:42, 12"
date: "2017-04-01"
hour: 11
Expected Output:
727
Where the first value is the date, the second one the time and the third one the amount of coins spent.

在给出文件的内容,特定日期和小时(只是小时,没有分钟)时,公司想知道在那个小时内花了多少钱。     我尝试按如下方式完成此任务:

int MapReduceProblem(String fileContent, String date, int hour) {
    int coins;
    coins = 0;
    if(fileContent.contains(";")){
        String[] fileLines = fileContent.split("\\;");
        for(int i = 0; i <= fileLines.length; i++){
          String[] lineData = fileLines[i].split("\\,");
          String[] hourSplit = lineData[1].split("\\:");
          int fileHour = Integer.parseInt(hourSplit[0]);
            if(fileHour == hour){
                int coin = Integer.parseInt(lineData[2]);
                coins += coin;
            }
        }
        return coins;
    }
    else{
        String[] lineData = fileContent.split("\\,");
        String[] hourSplit = lineData[1].split("\\:");
        int fileHour = Integer.parseInt(lineData[0]);
            if(fileHour == hour){
                int coin = Integer.parseInt(hourSplit[2]);
                coins += coin;
            }
         return coins;
    }

}

我从主要文件中得到错误:

Exception in thread "main" java.lang.AssertionError: java.lang.reflect.InvocationTargetException
    at myCode._invoke(file.java on line ?)
    at myCode.main(file.java on line ?)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at myCode._invoke(file.java on line ?)
    ... 1 more
Caused by: java.lang.NumberFormatException: For input string: "2017-04-04"

问题是我无法访问主要内容(这是一个练习题,这就是我无法访问主要内容的原因)。    我是java的新手,所以如果你能尝试向我解释我失败的原因,我将不胜感激。    谢谢大家的时间!

0 个答案:

没有答案