gpt4 book ai didi

java - 从文本文件中过滤的数据的增量日期

转载 作者:行者123 更新时间:2023-12-01 15:58:27 26 4
gpt4 key购买 nike

public class Reader {
public static void main(String[] args) throws IOException, ParseException {
BufferedReader reader;
String animalName="cat";
String animal = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream("C:/dila.txt")));
Map<String, Integer> result = new LinkedHashMap<String, Integer>();
Map<String, Integer> result2 = new LinkedHashMap<String, Integer>();

while (reader.ready()) {
String line = reader.readLine();
/split a line with spaces/
String[] values = line.split(",");
String key = null;
if(values[1].compareTo(animalName)==0){
key = values[0];
animal=""+values[1].compareTo(animalName);
int sum = 0;
int count = 0;
/get a last counter and sum/
if (result.containsKey(key)) {
sum = result.get(key);
count = result2.get(key);
} else{
}
/increment sum a count and save in the map with key/
result.put(key, sum + Integer.parseInt(values[2]));
result2.put(key, count + 1);
}
}

/interate and print new output/
for (String key : result.keySet()) {
Integer sum = result.get(key);
Integer count = result2.get(key);
System.out.println(key +" "+animalName+ " " + sum + "\t" + count);
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
  • 我有以下文本文件
    2010年11月2日,猫,6
    2010年11月2日,猫,3
    2010 年 11 月 2 日,狗,4
    2010年11月2日,猫,11
    2010 年 11 月 3 日,猫,1
    2010 年 11 月 3 日,狗,3
    2010年11月3日,猫,8
    2010年11月3日,猫,80

  • 在我的 java 代码中,它在输出下方被过滤。
    2010 年 11 月 2 日    猫    20    3
    2010 年 11 月 3 日    猫    104    4
    2010 年 11 月 4 日    猫    26    2

  • 但我想要下面提到的结果。
    2010年11月1日
    2010 年 11 月 2 日    猫    20    3
    2010 年 11 月 3 日    猫    104    4
    2010 年 11 月 4 日    猫    26    2
    2010年11月5日
    2010年11月6日
    2010年11月7日
    2010年11月8日
    2010年11月9日
    2010年10月11日
    2010年11月11日
    2010年11月12日
    2010年11月13日
    2010年11月14日
    2010年11月15日
    2010年11月16日
    2010年11月17日
    2010年11月18日
    2010年11月19日
    2010年11月20日
    2010年11月21日
    2010年11月22日
    2010年11月23日
    2010年11月24日
    2010年11月25日
    2010年11月26日
    2010年11月27日
    2010年11月28日
    2010年11月29日
    2010年11月30日

    上面显示了我的java代码...(我是这个网站的新手,这就是为什么我的问题对你来说有点不清楚。现在我得到了方法。现在我再次发布我的问题。所以请阅读这篇文章并给我解决方案.. ..)

最佳答案

这样做:

  1. 读取 txt 文件的第一行以获取月份。
  2. 创建一个月份大小的 HashMap 。使用 java.util.date 填充该月第一天到最后一天的日期。日期作为键,null 作为值。
  3. 读取文本文件,获取每行中的 CSV 的第一个,将其转换为 Date 对象
  4. 在 Hashmap 中进行查找,并将格式化字符串作为值插入相应的日期键。您可以使用 Formatter 进行填充。
  5. 重复 #3 和 #4 直到 EOF
  6. 现在,迭代 Hashmap 并打印出来。对于所有 NULL 值,只需打印格式化的 Key(即日期)

希望这有帮助。

 1. use SimpleDateFormat to convert date String to date object
2. write a method that takes this date and returns maximum days in that month using java.util.Calendar.getMaximum
3. create a hashMap with Keys as Dates in that month and values as Null
4. now, read line by line and user SimpleDateFormat to convert first of CSV into date. Use this date as key and put the printable String as value.
5. Iterate throught this hashmap.keys() and to get the values one by one. If the value is null just print the key i.e. date.

:)

无法提供更多帮助。

关于java - 从文本文件中过滤的数据的增量日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4534147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com