1,105,456 Community Members

String parsing

Member Avatar
gedas
Junior Poster
195 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi guys,

i have been having some problems parsing a list of strings.
lets say i have a list that has the following strings

List<String> list = new ArrayList<String>();

list.add("Process A Started");
list.add("Process B Started");
list.add("Process B Finished");
list.add("Process A Started");
list.add("Process C Started");
list.add("Process D Started");
list.add("Process C Finished");
list.add("Process A Finished");
list.add("Process B Started");
list.add("Process D Finished");
list.add("Process A Started");
...

what i need is to come up with the logic that would check whether the process A/B/C/D.. has started and has it finished. if it started and not finished that means it failed. The following would be the logic required:

  • If process started it has to finish

  • Process can not start again if it already started.

  • if a process started other process can run and finish before the specific process finished

please help me on sorting this out, suggestions, pseudo codes actual code :) or any other help would be appreciated.

Thanks You.

Member Avatar
stultuske
Posting Expert
5,343 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 777 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

well, first you would need to have actual processes, I suppose. could you just try and explain what exactly it is that you are trying to achieve?

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

Hi guys,

i have been having some problems parsing a list of strings.
lets say i have a list that has the following strings

List<String> list = new ArrayList<String>();

list.add("Process A Started");
list.add("Process B Started");
list.add("Process B Finished");
list.add("Process A Started");
list.add("Process C Started");
list.add("Process D Started");
list.add("Process C Finished");
list.add("Process A Finished");
list.add("Process B Started");
list.add("Process D Finished");
list.add("Process A Started");
...

what i need is to come up with the logic that would check whether the process A/B/C/D.. has started and has it finished. if it started and not finished that means it failed. The following would be the logic required:

  • If process started it has to finish

  • Process can not start again if it already started.

  • if a process started other process can run and finish before the specific process finished

please help me on sorting this out, suggestions, pseudo codes actual code :) or any other help would be appreciated.

Thanks You.

well adding to what stultuske said, if you create threads you could use the isAlive() method on the threads instance to see if its still alive. to make the process/thread end, you could look into the join(int time) method, or an instance variable which when the value is changed will make the thread quit.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
1
 

What does a list of Strings have to do with monitoring Threads?

Member Avatar
JamesCherrill
... trying to help
10,397 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

You could create a Map with process name as key and its status as value. The first time you process a record for a given process name you will add it to the Map with a status of "started". When you find its "finished" record you update the status. When all the data is processed you can check all the entries for status not "finished".
If you find a "started" record for a process that's already in the map then you know it's been started twice.

Member Avatar
gedas
Junior Poster
195 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanks a lot James, i see where you are going with this, however i am not comfortable working with hash maps( a while ago i was advised that they are not thread safe, therefore never learnt them).

However if you could expand on the second part of your comment:

When all the data is processed you can check all the entries for status not "finished".
If you find a "started" record for a process that's already in the map then you know it's been started twice.

i might be able to implement this ,but currently i am not very sure i understand how this could be achieved.

many thanks to everyone

Member Avatar
JamesCherrill
... trying to help
10,397 posts since Apr 2008
Reputation Points: 2,094 [?]
Q&As Helped to Solve: 1,752 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

HashMaps are a generally very useful tool - no need to avoid them! If you need thread-safe map then you can wrap the class - but there's nothing in your original post about thread-safe.
You wanted to know about processes that have started but not finished - these will be all the entries in the map that still have a status of "started" - you can find them with a simple loop thru the map.
You wanted to check that a process can not start again if it already started. That means when you try to add it to the Map it's already there. There's a map method for that.

Member Avatar
stultuske
Posting Expert
5,343 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 777 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

@NormR1: nothing at all. we were just pointing out that printing a String is not the same as starting a process

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article