0

I have a file (originally came from CSV file) that contains the two columns as following:

time     speed
11:49:29  10
11:49:29  12
11:49:30  50
11:49:32  60
11:49:33  80
11:49:34  80
11:49:35  90
11:49:35  90

I would like to add this to ArrayList and count the time in second and print only the first 10 seconds (or duration of 10 seconds). I wrote some code as follows:

// input time and speed reads from database/ CSV file

// add the speed to arraylist only for first 10 seconds
ArrayList<String> speedTimeTenSec = new ArrayList<>();

for (int i = 0; i < speed.size(); i++) {
    // for (time in first 10 second){
    speedTime.add(time.get(i));                    
    //} 
    System.out.print("speed in fisrt 10 sec:" + speedTime);            
}

I do not want to use like new ArrayList<>(10) or for i < 10 seconds as my input time file has many repeated seconds as seen above which for 10 seconds there are more than 10 entries(row/values).

expected output (speed in first (or duration of) 10 seconds ):

speed10Seconds:[10, 12, 50, 60, 80, 90, 90, 100, 110, 120, 130, 135, 140]

I thought I can subtract the time difference (and count the time as well) and referred to this or this, however, was not able to understand. Please let me know if you have any thoughts.

Bilgin
  • 499
  • 1
  • 10
  • 25
  • Can you show your expected output and what is package for `DoubleTimeSeries` ? – Eklavya Aug 25 '20 at 18:52
  • @Rono sure, i just updated the post. ```DoubleTimeSeries``` is just pars the csv to get the time and speed. – Bilgin Aug 25 '20 at 18:59
  • why don't you just keep a counter of elapsed seconds? Then you can just do `while(elapsedSeconds <= 10)` or keep track of `elapsedSeconds` inside of a `while(true)` loop, then you can `break;` if `elapsedSeconds` goes beyond your set threshold. – austin wernli Aug 25 '20 at 19:03
  • @Rono thanks for the comment. can you please show in example code that I can see how it should work and I also can acknowledge your answer? – Bilgin Aug 25 '20 at 19:07
  • To add to suggestion by @austinwernli: if you want to be a bit more adventurous, you could bind the time values to instances of classes from the java.time API. Then you can use Duration to work out the difference between different times. In this example, endTime and startTime may be LocalTime objects representing 11:49:35 and 11:49:29, respectively. The value of diff would be 6 and hence you can exit ny loop or conditional once this reaches 10: Duration duration = Duration.between(endTime, startTime); long diff = Math.abs(duration.toMinutes()); – Jon H Aug 25 '20 at 20:22
  • @JonH thanks for the comment. Yes, that will be more accurate in my case as the time can be parsed in java.time API and calculate the difference. Do you have any running code sample for it? – Bilgin Aug 25 '20 at 21:23

0 Answers0