We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,401 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

String parsing

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.

5
Contributors
8
Replies
3 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
gedas
Junior Poster
195 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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.

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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

gedas
Junior Poster
195 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

JamesCherrill
... trying to help
Moderator
8,527 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24
NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1141 seconds using 2.74MB