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

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

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

问题

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

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

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

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

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

  1. 2020-07-29 00:00:00
  2. 2020-07-29 01:00:00
  3. 2020-07-29 02:00:00
  4. 2020-07-29 03:00:00
  5. 2020-07-29 04:00:00
  6. 2020-07-29 05:00:00
  7. 2020-07-29 06:00:00
  8. 2020-07-29 07:00:00
  9. 2020-07-29 08:00:00
  10. 2020-07-29 10:00:00
  11. 2020-07-29 11:00:00
  12. 2020-07-29 12:00:00
  13. 2020-07-29 13:00:00
  14. 2020-07-29 14:00:00
  15. 2020-07-29 15:00:00
  16. 2020-07-29 16:00:00
  17. 2020-07-29 17:00:00
  18. 2020-07-29 18:00:00
  19. 2020-07-29 19:00:00
  20. 2020-07-29 20:00:00
  21. 2020-07-29 22:00:00
  22. 2020-07-29 23:00:00
  23. 2020-07-30 00:00:00
  24. 2020-07-30 01:00:00
  25. 2020-07-30 02:00:00
  26. 2020-07-30 03:00:00
  27. 2020-07-30 04:00:00
  28. 2020-07-30 05:00:00
  29. 2020-07-30 06:00:00
  30. 2020-07-30 07:00:00
  31. 2020-07-30 08:00:00
  32. 2020-07-30 09:00:00
  33. 2020-07-30 10:00:00
  34. 2020-07-30 11:00:00
  35. 2020-07-30 12:00:00
  36. 2020-07-30 13:00:00
  37. 2020-07-30 14:00:00
  38. 2020-07-30 15:00:00
  39. 2020-07-30 16:00:00
  40. 2020-07-30 17:00:00
  41. 2020-07-30 18:00:00
  42. 2020-07-30 19:00:00
  43. 2020-07-30 20:00:00
  44. 2020-07-30 21:00:00
  45. 2020-07-30 22:00:00
  46. 2020-07-30 23:00:00
  47. 2020-07-31 00:00:00
  48. 2020-07-31 01:00:00
  49. 2020-07-31 02:00:00
  50. 2020-07-31 03:00:00
  51. 2020-07-31 05:00:00
  52. 2020-07-31 06:00:00
  53. 2020-07-31 07:00:00
  54. 2020-07-31 08:00:00
  55. 2020-07-31 09:00:00
  56. 2020-07-31 10:00:00
  57. 2020-07-31 11:00:00
  58. 2020-07-31 13:00:00
  59. 2020-07-31 14:00:00
  60. 2020-07-31 15:00:00
  61. 2020-07-31 16:00:00
  62. 2020-07-31 17:00:00
  63. 2020-07-31 18:00:00
  64. 2020-07-31 19:00:00
  65. 2020-07-31 20:00:00
  66. 2020-07-31 21:00:00
  67. 2020-07-31 22:00:00
  68. 2020-07-31 23:00:00
  69. 2020-08-01 00:00:00
  70. 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:

  1. public class CSVReader {
  2. public static void main(String[]args) {
  3. String csvFile = "data.csv";
  4. BufferedReader br = null;
  5. String line = "";
  6. String cvsSplitBy = ";";
  7. try {
  8. br = new BufferedReader(new FileReader(csvFile));
  9. while ((line = br.readLine()) != null) {
  10. String[]text = line.split(cvsSplitBy);
  11. System.out.println(text[0]);
  12. }
  13. } catch (FileNotFoundException e) {
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. } finally {
  18. if (br != null) {
  19. try {
  20. br.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }
  27. }

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.

  1. 2020-07-30 00:00:00
  2. 2020-07-30 01:00:00
  3. 2020-07-30 02:00:00
  4. 2020-07-30 03:00:00
  5. 2020-07-30 04:00:00
  6. 2020-07-30 05:00:00
  7. 2020-07-30 06:00:00
  8. 2020-07-30 07:00:00
  9. 2020-07-30 08:00:00
  10. 2020-07-30 09:00:00
  11. 2020-07-30 10:00:00
  12. 2020-07-30 11:00:00
  13. 2020-07-30 12:00:00
  14. <- missing meter reading at 13:00:00
  15. 2020-07-30 14:00:00
  16. 2020-07-30 15:00:00
  17. 2020-07-30 16:00:00
  18. 2020-07-30 17:00:00
  19. 2020-07-30 18:00:00
  20. 2020-07-30 19:00:00
  21. 2020-07-30 20:00:00
  22. 2020-07-30 21:00:00
  23. 2020-07-30 22:00:00
  24. 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:

  1. 2020-07-29 00:00:00
  2. 2020-07-29 01:00:00
  3. 2020-07-29 02:00:00
  4. 2020-07-29 03:00:00
  5. 2020-07-29 04:00:00
  6. 2020-07-29 05:00:00
  7. 2020-07-29 06:00:00
  8. 2020-07-29 07:00:00
  9. 2020-07-29 08:00:00
  10. 2020-07-29 10:00:00
  11. 2020-07-29 11:00:00
  12. 2020-07-29 12:00:00
  13. 2020-07-29 13:00:00
  14. 2020-07-29 14:00:00
  15. 2020-07-29 15:00:00
  16. 2020-07-29 16:00:00
  17. 2020-07-29 17:00:00
  18. 2020-07-29 18:00:00
  19. 2020-07-29 19:00:00
  20. 2020-07-29 20:00:00
  21. 2020-07-29 22:00:00
  22. 2020-07-29 23:00:00
  23. 2020-07-30 00:00:00
  24. 2020-07-30 01:00:00
  25. 2020-07-30 02:00:00
  26. 2020-07-30 03:00:00
  27. 2020-07-30 04:00:00
  28. 2020-07-30 05:00:00
  29. 2020-07-30 06:00:00
  30. 2020-07-30 07:00:00
  31. 2020-07-30 08:00:00
  32. 2020-07-30 09:00:00
  33. 2020-07-30 10:00:00
  34. 2020-07-30 11:00:00
  35. 2020-07-30 12:00:00
  36. 2020-07-30 13:00:00
  37. 2020-07-30 14:00:00
  38. 2020-07-30 15:00:00
  39. 2020-07-30 16:00:00
  40. 2020-07-30 17:00:00
  41. 2020-07-30 18:00:00
  42. 2020-07-30 19:00:00
  43. 2020-07-30 20:00:00
  44. 2020-07-30 21:00:00
  45. 2020-07-30 22:00:00
  46. 2020-07-30 23:00:00
  47. 2020-07-31 00:00:00
  48. 2020-07-31 01:00:00
  49. 2020-07-31 02:00:00
  50. 2020-07-31 03:00:00
  51. 2020-07-31 05:00:00
  52. 2020-07-31 06:00:00
  53. 2020-07-31 07:00:00
  54. 2020-07-31 08:00:00
  55. 2020-07-31 09:00:00
  56. 2020-07-31 10:00:00
  57. 2020-07-31 11:00:00
  58. 2020-07-31 13:00:00
  59. 2020-07-31 14:00:00
  60. 2020-07-31 15:00:00
  61. 2020-07-31 16:00:00
  62. 2020-07-31 17:00:00
  63. 2020-07-31 18:00:00
  64. 2020-07-31 19:00:00
  65. 2020-07-31 20:00:00
  66. 2020-07-31 21:00:00
  67. 2020-07-31 22:00:00
  68. 2020-07-31 23:00:00
  69. 2020-08-01 00:00:00
  70. 2020-08-01 01:00:00

答案1

得分: 1

  1. int count = 24;
  2. while ((line = br.readLine()) != null) {
  3. String[] text = line.split(cvsSplitBy);
  4. if (text[0] != null && text[0].length > 1) {
  5. System.out.println(text[0]);
  6. counter--;
  7. } else {
  8. System.out.println("MISSING ENTRY");
  9. }
  10. }
  11. System.out.println("Missing entries: " + counter);
  12. I think of something like this to get a better view. Does it fix your problem?
英文:

...

  1. int count = 24;
  2. while((line=br.readLine()) != null){
  3. String[] text = line.split(cvsSplitBy);
  4. if(text[0] != null && text[0].length > 1){
  5. System.out.println(text[0]);
  6. counter--;
  7. } else {
  8. System.out.println("MISSING ENTRY");
  9. }
  10. }
  11. 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:

确定