【发布时间】:2014-08-21 23:24:04
【问题描述】:
这是我得到的代码。
Scanner scanner = new Scanner(new File("file name"));
ArrayList<Date> dateList = new ArrayList<Date>();
ArrayList<Date> timeList = new ArrayList<Date>();
ArrayList<String> consumptionList = new ArrayList<String>();
DateFormat sdf = new SimpleDateFormat ("HH:mm");
SimpleDateFormat dateInWeekday = new SimpleDateFormat ("MM/dd/yy");
while(scanner.hasNextLine()){
//read the line
String currentLine = scanner.nextLine();
String[] tokens = currentLine.split(",");
//string of each value
String date = tokens [0];
String time = tokens [1];
String consumption = tokens [2];
//convert Str time to date format
Date timeDateFormat = sdf.parse(time);
timeList.add(timeDateFormat);
consumptionList.add(consumption);
dateList.add(dateInWeekday.parse(date));
}
scanner.close();
double dayTotal = 0;
int count = 0;
for(int i = 0; i < dateList.size(); i++){
Calendar c = Calendar.getInstance();
c.setTime(dateList.get(i));
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if ((dayOfWeek == 1) || (dayOfWeek == 7)){
dayTotal += Double.parseDouble(consumptionList.get(i));
count ++;
}
}
}
}
所以,我希望结果是,例如: 2014 年 1 月 10 日 342 千瓦时 2014 年 1 月 11 日 292 千瓦时 我的数据是每 15 分钟的消耗量,所以我想要每天的总消耗量
【问题讨论】:
-
到目前为止您尝试过什么?发布一些代码,以便其他人可以帮助您。
-
那是一个数组吗?因为它看起来像多个数组。贴一些代码,因为很难理解你需要什么。
-
这,小伙子,不是数组。
-
请添加包含在文件中的示例数据。
标签: java arrays sorting arraylist