aspose file tools*
The moose likes Beginning Java and the fly likes Algorithm-Problem: Manipulate a line that depends on the next line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Algorithm-Problem: Manipulate a line that depends on the next line" Watch "Algorithm-Problem: Manipulate a line that depends on the next line" New topic
Author

Algorithm-Problem: Manipulate a line that depends on the next line

Daniel Palombo
Greenhorn

Joined: Feb 28, 2006
Posts: 10
Hello to all,
as the subject tells you, I want to manipulate some text of wich lines have interdependent informations.

Example: my textfile consists of lines that show a line-number, a start-timestamp, usually no end-timestamp and a text (it's something like a subtitel script).
1 s000000 "Blabla Inc. proudly presents:"
2 s000008 "a blabla-film production with"
3 s000015 e000020 "...blabla ..."

The third line is a correct line, so line 1 and 2 must be manipulatet. The algorithm should say something like "take a line and, if it has no end-timestamp, take the next line and set its start-timestamp as end-timestamp of the last line".

So my aproach is to put all lines into an array and walk through with a for-loop.

==> ArrayIndexOutOfBoundsException
This can't work, because at the end of the array I reference to the index i+1, which dosn't exist.

So my second aproach was this:


This works, but the very last line of the array is ommitted from processing (which always works on lastLine, not on nextLine, which in the end holds the very last line of the text so the for-loop exits without shifting it to lastLine).

I think that this could be a very common problem (that could be expanded from a two-line to a three- or more-lineProblem), but I haven't found a smart solution yet.

Is there a standard-solution for problems like this, or has anyone an idea how to solve this?

I'd like to apreciate your help!
Thanks a lot!
Daniel Palombo
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11422
    
  16

how are you supposed to process the last line in the file? if it has an end timestamp, you need to do nothing, right?

and if it DOESN'T have an end timestamp, how do you know what to put there?

what you could do is just run your original loop like this:



then, outside of the loop, process the last element in the array...
[ March 02, 2006: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can you process them backwards?

You might raise an error if you want to use a remembered end date and there is none.

We had a question a while ago about reading a file backwards. I still think a reverse buffered reader would be fun.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Martin Mathis
Ranch Hand

Joined: Dec 20, 2004
Posts: 45
I think it'd be a lot easier if you did it while reading the lines, instead of doing all the reading up front.

If you keep reading (and storing them) until you get to one that has an end timestamp, you know that every line in your array/list/etc... needs to be linked. So do the linking, removing them, and go back to reading some more.
Daniel Palombo
Greenhorn

Joined: Feb 28, 2006
Posts: 10
@fred rosenberger
Yes, thats exactly what I'am doing at the moment. It's obvious that this doubles a part of the code, and - worse - if I change something in the loop that affects a line, I have to remeber this also for the part that processes the last line outside of the loop: I think that is not so good.

I noticed, that if I say

I can avoid the ArrayIndexOutOfBoundsException though the problem with the last line still remains. But it is safer and more like the for (String line : allLines) {...} construction, but without the need of a jumppoint and the possibility to peek at any value in the array.
By the way, the very last line gets a default duration added to the start-timestamp, if it hasn't one.

Stan James' approach remembering the timestamp sounds interesting. Could you exemplify this approach, Stan? As you see above, the manipulation (and with it the parsing of the line) occurs in a separate method. So the loop itself doesn't know anything about timestamps at this moment. It would be a good idea to not to be forced to parse a line more than neccessary (and I'm doing this in the manipulateLine method).
It's in my opinion also a question of 'responsibilities': the manipulation method is responsible to know what to do with the content, not the method calling it, which in contrast only should be aware of "to subsequent lines go together".


We had a question a while ago about reading a file backwards. I still think a reverse buffered reader would be fun.

What are the advantages of this? Could you point out some informational places?

@Martin Mathis
Ashamed I must admit that I haven't understood what you mean. I thought that's what I'am doing: reading (two lines), linking (?, processing them interdependent), go back storing and continuing reading the next one.

Thank you (all) very much for your interest ...
Daniel Palombo
@fred rosenberger
Yes, thats exactly what I'am doing at the moment. It's obvious that this doubles a part of the code, and - worse - if I change something in the loop that affects a line, I have to remeber this also for the part that processes the last line outside of the loop: I think that is not so good.

I noticed, that if I say

I can avoid the ArrayIndexOutOfBoundsException though the problem with the last line still remains. But it is safer and more like the for (String line : allLines) {...} construction, but without the need of a jumppoint and the possibility to peek at any value in the array.
By the way, the very last line gets a default duration added to the start-timestamp, if it hasn't one.

Stan James' approach remembering the timestamp sounds interesting. Could you exemplify this approach, Stan? As you see above, the manipulation (and with it the parsing of the line) occurs in a separate method. So the loop itself doesn't know anything about timestamps at this moment. It would be a good idea to not to be forced to parse a line more than neccessary (and I'm doing this in the manipulateLine method).
It's in my opinion also a question of 'responsibilities': the manipulation method is responsible to know what to do with the content, not the method calling it, which in contrast only should be aware of "to subsequent lines go together".


We had a question a while ago about reading a file backwards. I still think a reverse buffered reader would be fun.

What are the advantages of this? Could you point out some informational places?

@Martin Mathis
Ashamed I must admit that I haven't understood what you mean. I thought that's what I'am doing: reading (two lines), linking (?, processing them interdependent), go back storing and continuing reading the next one.

Thank you (all) very much for your interest ...
Daniel Palombo
Martin Mathis
Ranch Hand

Joined: Dec 20, 2004
Posts: 45
I guess I was thinking more along the lines of something like this...



Don't know if there's something wrong there or not, just whipped it up really quick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Algorithm-Problem: Manipulate a line that depends on the next line