[Logo]
Forums Register Login
Implementing an algorithm using counters
Hi,

I have a file that looks like this:



I have extracted the values in each column and stored them in separate ArrayLists. Now I want to implement an algorithm that checks if an action is valid - by checking if the previous action that is being executed has finished its execution. My idea runs along the following lines, but I'm not sure how to start programming it. Could you please give me some directions?
0.000  count1 = 0.001
0.001  count1=0         count2= 0.001
0.002                         count2=0         count3=0.001
0.003                                                count3=0
     



 

if the previous action that is being executed has finished its execution


Can the class that holds the action have a flag or state variable to remember that?


  stored them in separate ArrayLists.


Why separate?  Are you looking at parallel lists?  A bad idea.  Build a class to hold all the bits needed.
 

Norm Radder wrote:Can the class that holds the action have a flag or state variable to remember that?


The thing is since the file can be changed, i.e. another file of the same format might be used, I never know if a previous action had finished execution.

Why separate?  Are you looking at parallel lists?  A bad idea.  Build a class to hold all the bits needed.


I tried that but since I am reading the file using a process, and then extracting the parts using different regexes, and storing them into arrays, I don't know how to build a separate class that use my code that reads the file, uses pattern matcher etc. How can I do that?
 

the file can be changed


Not sure what that means.  Can the file be changing while you are reading it?  In other words, if you read the file into memory, the contents of the file can be changed and what was read is no longer valid.

I never know if a previous action had finished execution. 


Isn't that a requirement?  If you can't tell that, how can you  start any actions?
You said:


by checking if the previous action that is being executed has finished




You need to spend more time with the overall design and less time posting examples of file contents.
 

Norm Radder wrote:Not sure what that means.  Can the file be changing while you are reading it?  In other words, if you read the file into memory, the contents of the file can be changed and what was read is no longer valid.


Sorry, that was unclear. It means that I might have to read a different file with the same structure, but it never changes during reading.

Isn't that a requirement?  If you can't tell that, how can you  start any actions?


Yes, it is a requirement, but checks still need to be done in order to be sure that this is the case. So basically even if a file supposedly contains only valid actions, I still need to check if every single one of them is valid.
Can you describe what the three columns of data in the file is and how the three columns are related?

need to check if every single one of them is valid. 


What needs to be tested for validity?
 

Norm Radder wrote: Can you describe what the three columns of data in the file is and how the three columns are related?


Each line is one action - first column is start time, second column is "name of action" and 3rd one is the duration

What needs to be tested for validity?


What I said in my first post - it is valid if while say the first action is valid, there isn't any other actions that are interrupting it. For example if the duration of the first action was 0.002 and at time 0.001 there was another action to be executed, the second action is not valid, since the first action finishes  at time 0.002 and only at time 0.002 or later the next action can be executed.
Ok, what is your algorithm for detecting if an action is valid?
What happens to invalid actions?  Rejected and ignored or ???

What is The final object:  a report listing the actions and whether they are valid or not?
 

Norm Radder wrote:Ok, what is your algorithm for detecting if an action is valid?
What happens to invalid actions?  Rejected and ignored or ???


My idea was the 'pseudocode' I posted in my first post, basically initialising a counter every time a new action is executed and decrement it and at time start time+duration time it becomes 0, so that means it finished, and then keep initialising new counters with the duration value. But I'm not sure how to keep track of each separate actions so that I can use that information to perform the checks. Also, since you said I should make an object with all the attributes - I made it a static class inside the class where I'm processing the file but my question is - how would I populate those attributes so that every new action is stored with all its attributes - with its name, startTime and duration?
Invalid actions are rejected.

Norm Radder wrote:What is The final object:  a report listing the actions and whether they are valid or not?


Yes, for now that is enough.
Not sure what the counter is for.
Isn't testing the next action for validity a simple compare of its start time against the current action's start time plus duration?  The start time must be afterwards.
 

Norm Radder wrote:Not sure what the counter is for.
Isn't testing the next action for validity a simple compare of its start time against the current action's start time plus duration?  The start time must be afterwards.


Yes, but still my question stands - how would I keep track of all the attributes of each action, meaning  when I need check the previous action vs current action times, how do I know which action is previous and which is current one? I was thinking of using HashMaps but they they are only two dimensional?
 

  keep track of all the attributes of each action, meaning  when I need check the previous action vs current action times, how do I know which action is previous


Read the records for the actions, create objects for each action, store the action objects in a list, sort the list by starting time.
When working on any action from the list, the previous one would be at a lesser index.
 

Norm Radder wrote:Read the records for the actions, create objects for each action, store the action objects in a list, sort the list by starting time.
When working on any action from the list, the previous one would be at a lesser index.


Thanks, that makes sense but there's something else -  while I'm reading the file, I've filtered the lines that I need and stored them in an ArrayList and then I'm looping through that list and extracting the all the columns separately. If I have an object for each line(action) then I am going to lose the distinction between start and duration time for example. So basically where would my regex manipulation go in my code?
All the data from a line should go into one object and that object can be saved in a list.


where would my regex manipulation go


Finish getting the design work done before writing any code.
 

Norm Radder wrote:All the data from a line should go into one object and that object can be saved in a list.


Yes, I get that, but I am unsure as to how I would 'remember' each attribute of each line separately for later sorting the list and comparing the time+duration?


Finish getting the design work done before writing any code.


I do already have code that extracts the different columns is what I meant and if I store each line as a new object, I would lose my regex manipulation, e.g. extracting the first column(decimal number) etc.
 

'remember' each attribute of each line separately


Time to take a look at OOP programming. 

When a line is read from the file, it is parsed into the 3 columns and that data is used to create an object.  That object is added to a list.
All the attributes of each line are in the object that is saved in the list in the same order they were in the input file.

comparing the time+duration? 


That can be a field in the object holding all the line's data
 

Norm Radder wrote: When a line is read from the file, it is parsed into the 3 columns and that data is used to create an object.  That object is added to a list.
All the attributes of each line are in the object that is saved in the list in the same order they were in the input file.


Okay, I did some research and all solutions I saw parse it using split, but as I've said, I am using regex to extract the values; simply using split by some delimiter won't work for me. So that was my initial confusion - I understand how I would do it using the split method but not using Pattern and Matcher.
So this is a new problem: How to extract the 3 pieces of data from an input record:


Each line is one action - first column is start time, second column is "name of action" and 3rd one is the duration


There are probably several ways to parse the input line:
split
regex
other String methods like indexOf
If the first column always ends with a space and the last column always follows a space, something as simple as the String class's indexOf method could work.

How to parse the data from a line is a small part of the total project.
Suppose you ae given the segments (1, 5) and (2,3), in a list that has sorted these intervals on start value. Now it is given the no interval may have a starting value that is before the ending value of a predecessor. Now, the end value of the first interval is 5, and the end value of the second one is 3. So, that will lead to the skipping of the second interval.

If the second interval was (2, 7), then it must be adjusted to (5, 7) since it cannot begin before the end of the previous interval.

If the second interval was (7, 12), then there is no need to adjust the intervals, they are already oke.

Now, going from first interval to the last, do you see what value you have to keep in mind while traversing the list? And for a suitable class to create objects from the splitted lines: have another look at the suggestion that Stephan made in the previous topic. It boils down to a class that has an begin duration (measured from the start of whatever needs to be undertitled, and comparable to the startig value of my interval above), it has  a duartion telling how long the subtitle should last, and the end duration is the sum of these two durations. Keep that in mind while looking at the next record.
 

Norm Radder wrote:So this is a new problem: How to extract the 3 pieces of data from an input record:


Each line is one action - first column is start time, second column is "name of action" and 3rd one is the duration


There are probably several ways to parse the input line:
split
regex
other String methods like indexOf
If the first column always ends with a space and the last column always follows a space, something as simple as the String class's indexOf method could work.


The parsing is not a problem for me, as I said I've already done that part using regex, but my question was how would I parse using regex and still store each line in an object?
From the solutions I've seen online all of them use either split or substring.
 

The parsing is not a problem for me, as I said I've already done that part using regex, 


I'm confused.  If you are able to parse out the three columns of data, what further problems do you have? 
Given the three parts from the line, an object to save them would involve calling the object's constructor and passing it the 3 parts.

Please post a small simple program with a String holding a sample record and the code you are using to parse that input String into the three separate parts.
(1 like)
But what is then the problem? If you have split a line into three components: start duration, text, showing duration (with the help of either a regex, string.split, or otherwise), then you can create an object with these three parameters (remember two topics ago I suggested a method splitAndSave to do this, that lead unfortunately to confusion?). store these in a list, sort them on starting duration and off you go.
 

Norm Radder wrote: I'm confused.  If you are able to parse out the three columns of data, what further problems do you have? 
Given the three parts from the line, an object to save them would involve calling the object's constructor and passing it the 3 parts.

Please post a small simple program with a String holding a sample record and the code you are using to parse that input String into the three separate parts.


The problem is that I don't know how to use the parsed data to be stored in my objects;
Change your testing code to just print out each of the three values so we can easily see that they have been parsed.

Note: I did the above and the posted code did not work. 
1) had compiler error
2) did not find the second time
What objects are those, then? Right now you're parsing the String, I assume correctly, but you're just adding the parsed data to three ArrayLists. But you should have a class which encapsulates those three parts, shouldn't you? So far I don't believe we've seen a declaration of any such class in what you have posted.
A simplified class could be:

Now, for ech line to split: determine start, text and duration, create a new instance by calling 'Subtitle instance = new Subtitle(start, text, duration) and add it to the list.
After the last processed line start the process of adjusting the start times.
 

Paul Clapham wrote:What objects are those, then? Right now you're parsing the String, I assume correctly, but you're just adding the parsed data to three ArrayLists. But you should have a class which encapsulates those three parts, shouldn't you? So far I don't believe we've seen a declaration of any such class in what you have posted.


I believe I've been saying that all afternoon.  Maybe a few more times will sink in.
 

Norm Radder wrote:Change your testing code to just print out each of the three values so we can easily see that they have been parsed.

Note: I did the above and the posted code did not work. 
1) had compiler error
2) did not find the second time


Ooops, sorry, of course not. I was copying my code and forgot to just print them, instead of adding one value to an arrayList.

Paul Clapham wrote:What objects are those, then? Right now you're parsing the String, I assume correctly, but you're just adding the parsed data to three ArrayLists. But you should have a class which encapsulates those three parts, shouldn't you? So far I don't believe we've seen a declaration of any such class in what you have posted.


I didn't say I was posting the code with objects, this was just my initial code that I already had. That's what my confusion comes from. How do I encapsulate those parts in an object?
 

Trent Green wrote:How do I encapsulate those parts in an object?



The class which Piet posted about three or four posts back should do for a start.
 

Paul Clapham wrote:The class which Piet posted about three or four posts back should do for a start.


Okay, maybe I didn't word my question correctly. I know how to create such a simple class, but I thought that the encapsulation of the attributes should be in the part of the code where I read my file and parse it using regex, or am I wrong?
(1 like)
 

Trent Green wrote:I know how to create such a simple class, but I thought that the encapsulation of the attributes should be in the part of the code where I read my file and parse it using regex, or am I wrong?


Encapsulation would presumably happen in the class Piet showed as an example, not in the places near or around where you use regex.
Seems like you have too much mental connection with regex, much simpler would be with combination of indexOf and substring methods.

What you have in your code example at lines 7, 14 and 21 would go away, and instead adding these pieces to their own data structures, you'd create object (with these 3 parameters) and add once at the end of your regex magic.
 

  I was copying my code


How does your compiler handle the (( on line 10?  My compiler says there is an error there.
The class is the thing which encapsulates the attributes. Encapsulation is a design feature, not a programming feature. The class acts as a "capsule" which contains certain information and controls access to it.

Now if your question was whether you should parse the data into its components and pass them to the class separately, or whether you should pass the unparsed string to an object of the class and have it do the parsing, well, again that's a design question. If your class is just a simple container for the data items, then it shouldn't be responsible for parsing them out of a particular format. However if your class is intended to know everything there is to know about, about whatever those three things are supposed to represent, then you might want to have it do the parsing.

Right now if I were you I'd just go for the simple container idea. It may be that as time goes on you may find a need for something more complex which manages the relationships among objects of that class, and so on. Such requirements would usually come out of a design process.
 

Paul Clapham wrote:Now if your question was whether you should parse the data into its components and pass them to the class separately, or whether you should pass the unparsed string to an object of the class and have it do the parsing, well, again that's a design question. If your class is just a simple container for the data items, then it shouldn't be responsible for parsing them out of a particular format. However if your class is intended to know everything there is to know about, about whatever those three things are supposed to represent, then you might want to have it do the parsing.

Right now if I were you I'd just go for the simple container idea. It may be that as time goes on you may find a need for something more complex which manages the relationships among objects of that class, and so on. Such requirements would usually come out of a design process.


Yes, that's exactly what I was asking.
I'm just posting some random code I found online:

Basically here, since the parsing was just done by using split, it's obvious how to parse the data to the attributes. And don't get me wrong, my heart really isn't set on using regex, but I have a lot of lines in my text file and think regex would be the way to go. So, in my case, parsing to the attributes won't be as easy and that is what I can't figure out how to do.
 

  parsing to the attributes won't be as easy and that is what I can't figure out how to do.


Will this work:


If the first column always ends with a space and the last column always follows a space, something as simple as the String class's indexOf method could work.


Find the first space, use substring to get what is before it.  Use lastIndexOf to find the last space and use substring to get what is after it.
 

Norm Radder wrote:Will this work:


If the first column always ends with a space and the last column always follows a space, something as simple as the String class's indexOf method could work.


Find the first space, use substring to get what is before it.  Use lastIndexOf to find the last space and use substring to get what is after it.


Unfortunately not. The first column always ends with a colon and the last column isn't necessarily followed by a space.
I'm at a loss here. The splitting was not the problem, was it? The splitting you gave a few posts ago concerns a complete other format, I really don't see what, after three topics, still is the problem. Throwing in the towel.
 

Piet Souris wrote:I'm at a loss here. The splitting was not the problem, was it? The splitting you gave a few posts ago concerns a complete other format, I really don't see what, after three topics, still is the problem. Throwing in the towel.


Sorry about that, but that's why I said regex works best because I get what I'm interested in: a double number, text inside brackets and another double. Although, from all the different posts here I get that regex can't(or at least it won't be easy) be used to parse the data in this case, can it?
I have moved the topic to Beginning Java forum. There probably it will get more attention.

Regarding data line format, your example was a bit misleading, because it showed 3 lines of the same kind. For the future keep in mind, that if data isn’t consistent, is much better to demonstrate that in the very first example, so the people who help you would be well informed.

Now that you gave some information but still didn’t provide example(s), it gets hard to tell something else.

Anyway, Trent, as been me tioned already, it is only the little detail in the project which most likely has various approaches, so I suggest don’t spend too much time on it as of now.

Much more useful would be if you’d identify steps and place them as headers in your code what you needed to do, so we could discuss them.

An example:
// to-do parse data
// create and store objects with a given data from parser
// sort objects by their start times

That is what you need to have in your head before you start writing regex’es.
If you got data parsed correctly by regex - that is fine! Let’s move on.

Do you understand what guys mentioned (including myself) about creating objects with a given data? That is kind of similar step what you do now, just instead of adding these three (number, action, number) to three separate data structures, you’d create object and would add once in a list or other data structure as WhateverItRepresents objects, by one for each parsed line.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 1354 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jun 18, 2018 07:57:14.