Java – 遍历CSV文件,根据时间戳检查缺失的仪表读数

huangapple go评论90阅读模式
英文:

Java - loop trough a csv file checking for missing meter readings based on the time stamp

问题

我已经有阅读器了。现在我卡在编写检查时间线中是否存在间隙的循环上。

我的目标是在控制台或外部 .txt 文件中获取整个文件的概览,看看是否有遗漏。

例如,输出应该是:2020-07-30: 13:00:00 缺少表计读数

因此,应列出每天缺少某些测量的日期和缺少的时间。希望我已经解释清楚我的问题,也希望有人可以帮助我。

此外,为了测试,以下是我的文件中的一些示例时间戳:

2020-07-29 00:00:00
2020-07-29 01:00:00
2020-07-29 02:00:00
2020-07-29 03:00:00
2020-07-29 04:00:00
2020-07-29 05:00:00
2020-07-29 06:00:00
2020-07-29 07:00:00
2020-07-29 08:00:00
2020-07-29 10:00:00
2020-07-29 11:00:00
2020-07-29 12:00:00
2020-07-29 13:00:00
2020-07-29 14:00:00
2020-07-29 15:00:00
2020-07-29 16:00:00
2020-07-29 17:00:00
2020-07-29 18:00:00
2020-07-29 19:00:00
2020-07-29 20:00:00
2020-07-29 22:00:00
2020-07-29 23:00:00
2020-07-30 00:00:00
2020-07-30 01:00:00
2020-07-30 02:00:00
2020-07-30 03:00:00
2020-07-30 04:00:00
2020-07-30 05:00:00
2020-07-30 06:00:00
2020-07-30 07:00:00
2020-07-30 08:00:00
2020-07-30 09:00:00
2020-07-30 10:00:00
2020-07-30 11:00:00
2020-07-30 12:00:00
2020-07-30 13:00:00
2020-07-30 14:00:00
2020-07-30 15:00:00
2020-07-30 16:00:00
2020-07-30 17:00:00
2020-07-30 18:00:00
2020-07-30 19:00:00
2020-07-30 20:00:00
2020-07-30 21:00:00
2020-07-30 22:00:00
2020-07-30 23:00:00
2020-07-31 00:00:00
2020-07-31 01:00:00
2020-07-31 02:00:00
2020-07-31 03:00:00
2020-07-31 05:00:00
2020-07-31 06:00:00
2020-07-31 07:00:00
2020-07-31 08:00:00
2020-07-31 09:00:00
2020-07-31 10:00:00
2020-07-31 11:00:00
2020-07-31 13:00:00
2020-07-31 14:00:00
2020-07-31 15:00:00
2020-07-31 16:00:00
2020-07-31 17:00:00
2020-07-31 18:00:00
2020-07-31 19:00:00
2020-07-31 20:00:00
2020-07-31 21:00:00
2020-07-31 22:00:00
2020-07-31 23:00:00
2020-08-01 00:00:00
2020-08-01 01:00:00
英文:

I've got an csv file full of meter reading of a whole month - 24 meter readings a day to be exactly. Now i want to check if there really are 24 meter readings a day, because somehow there are some that miss.

My code:

public class CSVReader {

    public static void main(String[]args) {

        String csvFile = "data.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ";";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {
                
                String[]text = line.split(cvsSplitBy);
                System.out.println(text[0]);

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

I've got the reader already done. Now i am stuck on programming the loop for checking if there is a gap in the timeline.

2020-07-30 00:00:00
2020-07-30 01:00:00
2020-07-30 02:00:00
2020-07-30 03:00:00
2020-07-30 04:00:00
2020-07-30 05:00:00
2020-07-30 06:00:00
2020-07-30 07:00:00
2020-07-30 08:00:00
2020-07-30 09:00:00
2020-07-30 10:00:00
2020-07-30 11:00:00
2020-07-30 12:00:00
                        <- missing meter reading at 13:00:00
2020-07-30 14:00:00
2020-07-30 15:00:00
2020-07-30 16:00:00
2020-07-30 17:00:00
2020-07-30 18:00:00
2020-07-30 19:00:00
2020-07-30 20:00:00
2020-07-30 21:00:00
2020-07-30 22:00:00
2020-07-30 23:00:00

My goal is it to get a kind of overview of that whole file in the console or in an external .txt file if something got missing.

For example the output:2020-07-30: missing meter reading at 13:00:00

So there should be listed every day where some measurements are missing and at the time where the are missing. I hope that i explained my problem and also i hope that there is somebody who can help me.

In addition here are some example timestamps in my file for testing reasons:

2020-07-29 00:00:00
2020-07-29 01:00:00
2020-07-29 02:00:00
2020-07-29 03:00:00
2020-07-29 04:00:00
2020-07-29 05:00:00
2020-07-29 06:00:00
2020-07-29 07:00:00
2020-07-29 08:00:00
2020-07-29 10:00:00
2020-07-29 11:00:00
2020-07-29 12:00:00
2020-07-29 13:00:00
2020-07-29 14:00:00
2020-07-29 15:00:00
2020-07-29 16:00:00
2020-07-29 17:00:00
2020-07-29 18:00:00
2020-07-29 19:00:00
2020-07-29 20:00:00
2020-07-29 22:00:00
2020-07-29 23:00:00
2020-07-30 00:00:00
2020-07-30 01:00:00
2020-07-30 02:00:00
2020-07-30 03:00:00
2020-07-30 04:00:00
2020-07-30 05:00:00
2020-07-30 06:00:00
2020-07-30 07:00:00
2020-07-30 08:00:00
2020-07-30 09:00:00
2020-07-30 10:00:00
2020-07-30 11:00:00
2020-07-30 12:00:00
2020-07-30 13:00:00
2020-07-30 14:00:00
2020-07-30 15:00:00
2020-07-30 16:00:00
2020-07-30 17:00:00
2020-07-30 18:00:00
2020-07-30 19:00:00
2020-07-30 20:00:00
2020-07-30 21:00:00
2020-07-30 22:00:00
2020-07-30 23:00:00
2020-07-31 00:00:00
2020-07-31 01:00:00
2020-07-31 02:00:00
2020-07-31 03:00:00
2020-07-31 05:00:00
2020-07-31 06:00:00
2020-07-31 07:00:00
2020-07-31 08:00:00
2020-07-31 09:00:00
2020-07-31 10:00:00
2020-07-31 11:00:00
2020-07-31 13:00:00
2020-07-31 14:00:00
2020-07-31 15:00:00
2020-07-31 16:00:00
2020-07-31 17:00:00
2020-07-31 18:00:00
2020-07-31 19:00:00
2020-07-31 20:00:00
2020-07-31 21:00:00
2020-07-31 22:00:00
2020-07-31 23:00:00
2020-08-01 00:00:00
2020-08-01 01:00:00

答案1

得分: 1

    int count = 24;
    while ((line = br.readLine()) != null) {
        String[] text = line.split(cvsSplitBy);
        if (text[0] != null && text[0].length > 1) {
            System.out.println(text[0]);
            counter--;
        } else {
            System.out.println("MISSING ENTRY");
        }
    }

    System.out.println("Missing entries: " + counter);

I think of something like this to get a better view. Does it fix your problem?
英文:

...

int count = 24;
while((line=br.readLine()) != null){
  String[] text = line.split(cvsSplitBy);
  if(text[0] != null && text[0].length > 1){
    System.out.println(text[0]);
    counter--;
  } else {
    System.out.println("MISSING ENTRY");
  }
}

System.out.println("Missing entries: " + counter);

...

I think of something like this to get a better view. Does it fix your problem?

huangapple
  • 本文由 发表于 2020年8月5日 20:39:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63265424.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定