I am reading from a file and want to split that file in two by a delimiter word - Duration, where I am interested only in the second part of the file. My question is, when I loop through the file I store the split in an array but then when I want to access that array and loop though it to get the second part of the file, I can't seem to find a way to access that array outside of the while loop:
a way to access that array outside of the while loop:
Define the array outside of the loop at the same scope as where you want to assess it
and assign it values inside of the loop.
If the code goes around the loop multiple times, only the last values assigned to the array will be in the array. Any previous values will be overwritten and gone.
I'm not sure why String.split must be used at all. If you have a BufferedReader, and you read in line by line, maintain a boolean 'beforeDuration', and only store the lines when that boolean is false. An ArrayList might be handier than an Array. Or did I misunderstand something?
Piet Souris wrote:I'm not sure why String.split must be used at all. If you have a BufferedReader, and you read in line by line, maintain a boolean 'beforeDuration', and only store the lines when that boolean is false. An ArrayList might be handier than an Array. Or did I misunderstand something?
I think I might have expressed myself a bit incorrectly - I am reading in files of the same structure where I don't know how many lines there are before the keyword Duration so I think I do need String.split , and what I actually have to do is to split the file/s after that line that contains Duration, followed by a decimal number too. So I am interested only in what comes after that line.
I am interested only in what comes after that line.
Use a String method like indexOf to detect that String.
Read and skip lines until a line with that String is found.
Piet Souris wrote:Exactly what I meant.
Okay, that makes sense, but since I need to use a regex since it is not just the word Duration, but as I mentioned, for example Duration 0.03, how would I store that in a String?
Describe in text what the reg exp is supposed to match. For example:
anything
followed by the String "XYZ"
followed by a single space
followed by 0 or more digits
and so on
Note: Read the API doc for the String class's methods to see which ones take a reg exp. I don't think indexOf() does.
Describe in text what the reg exp is supposed to match. For example:
anything
followed by the String "XYZ"
followed by a single space
followed by 0 or more digits
and so on
Note: Read the API doc for the String class's methods to see which ones take a reg exp. I don't think indexOf() does.
No, I actually meant the code that is supposed to "split" my file. I know what my regex has to do. And I was asking whether you think it seems correct.
Yes, you're right, I guess I can just store the regex in a variable and then use it with the String I use that stores my file contents. Do you think that will be ok?
The reg exp String can be stored in a variable and then used with any method that takes a reg exp.
I know what my regex has to do.
Ok, do you have a reg exp that does what you want?
I was suggesting the steps you need to take before trying to write a reg exp - list the parts of the String that are to be matched/skipped
Ok, do you have a reg exp that does what you want?
I was suggesting the steps you need to take before trying to write a reg exp - list the parts of the String that are to be matched/skipped
The one I have is the one i posted in my first post: Duration \\s.+\\n
What do you think? Thanks!
the code inside my while loop is not even being executed
Can you post the new code (wrapped in code tags) so we can see it?
Sure.
I did some modifications to my other code and it actually goes into the while loop but when I try to print index outside the if statement it is always -1, therefore it never goes into the if statement. This is weird and frustrating, because I do have a line in my file is of that regex form.
Did you copy and paste the code that you are working on?
Or was the posted code typed in?
The posted code doesn't look like it would compile.
There are two similar named variables: DurReg and timeReg. DurReg is not used and timeReg is not defined.
if statement it is always -1
That means the search arg is not found.
Have you read the API doc for the indexOf method? Does it take a reg exp as an argument?
Did you try some simple tests with a println statement like I showed earlier?
Note: the line read into str at line 7 will be lost immediately when another line is read at line 1
Norm Radder wrote:Did you copy and paste the code that you are working on?
Or was the posted code typed in?
The posted code doesn't look like it would compile.
There are two similar named variables: DurReg and timeReg. DurReg is not used and timeReg is not defined.
Have you read the API doc for the indexOf method? Does it take a reg exp as an argument?
Did you try some simple tests with a println statement like I showed earlier?
Oops, I was changing things and ended up posting the wrong version:
I did check the API and it indexOf doesn't take a regex per se but I thought I might be able to use it masked as a String.
if statement it is always -1
That means the search arg is not found.
But if my regex is correct and there is a line in my file of this form, how come it is not?
Note: the line read into str at line 7 will be lost immediately when another line is read at line 1
So instead of reading the line at the end, I think I should store the the part I want in an ArrayList, but I'm not too sure how I should get that? Should I add str.substring(index) to an initialised ArrayList?
But if my regex is correct and there is a line in my file of this form, how come it is not?
You can't just put a regex in anywhere you want and hope it works. Since indexOf() takes a String as its argument, you are literally searching for Duration \s. +\n.
So instead of reading the line at the end, I think I should store the the part I want in an ArrayList, but I'm not too sure how I should get that? Should I add str.substring(index) to an initialised ArrayList?
You're close. Assuming you've initialized list you can add str to it with list.add(str). But it seems that "the part I want" is the actual duration. This you still haven't figured out.
Hint 1: Do you need to use regexes?
Hint 2: If so, is there a String method that takes a regex?
Try it in a simple test program like I posted Yesterday 1:55:13 PM That will quickly show if it works and will easily allow you to experiment to see what works.
So instead of reading the line at the end, I think I should store the the part I want in an ArrayList, but I'm not too sure how I should get that? Should I add str.substring(index) to an initialised ArrayList?
You're close. Assuming you've initialized list you can add str to it with list.add(str). But it seems that "the part I want" is the actual duration. This you still haven't figured out.
Hint 1: Do you need to use regexes?
Hint 2: If so, is there a String method that takes a regex?
Now my problem is that I need to split the file immediately after the line containing the word Duration:
An example of the structure of the file:
Line 1
Line 2
Line 3
.
.
Duration 0.01
0.001: (some text)
0.001: (some text)
0.002: (some text)
Basically I need to extract the bold part but for now when I used, say String.split, the only thing I do is remove the line " Duration 0.01"
Try it in a simple test program like I posted Yesterday 1:55:13 PM
That will quickly show if it works and will easily allow you to experiment to see what works.
Yes, I did test it and it works, I was just saying it hypothetically.
Trent Green wrote:
Now my problem is that I need to split the file immediately after the line containing the word Duration:
An example of the structure of the file:
Line 1
Line 2
Line 3
.
.
Duration 0.01
0.001: (some text)
0.001: (some text)
0.002: (some text)
Basically I need to extract the bold part but for now when I used, say String.split, the only thing I do is remove the line " Duration 0.01"
I'm confused, because the Split() method operates on a String -- in your case, a line in the file. How can you use this to "split the file"?
Other questions: do all the lines that you want start with the pattern 0.000? Do any other lines you don't want start with that pattern?
Ok, if you have code that detects the line with the desired String,
what problems are you having now?
After finding the line with the desired String, the code would process the following lines up to some terminating condition like EOF or a line with a special String on it is read.
Knute Snortum wrote:
I'm confused, because the Split() method operates on a String -- in your case, a line in the file. How can you use this to "split the file"?
Other questions: do all the lines that you want start with the pattern 0.000? Do any other lines you don't want start with that pattern?
I was just thinking of using this as a indicator as to which lines to ignore - ignore up to the line starting with Duration and then read and store the rest of the lines
Yes, all of the lines I need are of this pattern and no, there aren't any lines of the same pattern beforehand.
Norm Radder wrote:
Ok, if you have code that detects the line with the desired String,
what problems are you having now?
After finding the line with the desired String, the code would process the following lines up to some terminating condition like EOF or a line with a special String on it is read.
I just think the problem right now is I've confused myself too much and don't know how to proceed next. I basically know my regex works but the splitting doesn't work.
Piet Souris wrote:In my first reply I hinted to this:
I don't understand why this simple code wouldn't at least be worth a try.
Sorry for neglecting your idea, it actually seems like a solution that might work for me. Just a question about the splitAndSave method you mentioned - I was wondering by what criteria should I split my whole file, because now it's not about the Duration but merely about splitting it in parts, isn't it? And then looping and storing them in an ArrayList?
Can you post a couple of sample uses of your regex to show the following 2 cases:
1) that it detects a String with a match
2) that it detects a String without a match
I was wondering by what criteria should I split my whole file, because now it's not about the Duration but merely about splitting it in parts, isn't it? And then looping and storing them in an ArrayList?
Piet's code does split the whole file. All lines before and including the one that starts with "Duration" are skipped. Isn't that what you mean by "splitting the file"?
I was wondering by what criteria should I split my whole file, because now it's not about the Duration but merely about splitting it in parts, isn't it? And then looping and storing them in an ArrayList?
Piet's code does split the whole file. All lines before and including the one that starts with "Duration" are skipped. Isn't that what you mean by "splitting the file"?
Yes, but I was asking about the method splitAndSave, which he included in the code, because I was unsure by what criteria it should split the whole file - perhaps by a whitespace?
The contents of the boolean variable: afterDuration is used to determine the split point in the file.
While it is false, the lines that are read are skipped.
When a line with the special String is read, afterDuration is set to be true
The remaining lines that are read are then passed to the splitAndSave method.
The name is misleading for me. The split point has already been determined by the code that found the special String in one of the lines that were read. I'd have called the method: saveTheLines.
Norm Radder wrote:The contents of the boolean variable: afterDuration is used to determine the split point in the file.
While it is false, the lines that are read are skipped.
When a line with the special String is read, afterDuration is set to be true
The remaining lines that are read are then passed to the splitAndSave method.
The name is misleading for me. The split point has already been determined by the code that found the special String in one of the lines that were read. I'd have called the method: saveTheLines.
Okay, how about this?
Also, sorry if I've been asking stupid questions but after that split, how do I access the lines that are stored in the saveLines method inside the while loop?
Norm Radder wrote:The contents of the boolean variable: afterDuration is used to determine the split point in the file.
While it is false, the lines that are read are skipped.
When a line with the special String is read, afterDuration is set to be true
The remaining lines that are read are then passed to the splitAndSave method.
The name is misleading for me. The split point has already been determined by the code that found the special String in one of the lines that were read. I'd have called the method: saveTheLines.
Okay, how about this?
Also, sorry if I've been asking stupid questions but after that split, how do I access the lines that are stored in the saveLines method inside the while loop?
Norm Radder wrote:The ArrayList variable: duration needs to be defined outside of the method so that it will be preserved from one call to the next.
Oh yeah, you're right, but still what do I have to do in order to print only the afterDuration lines? I changed the method to not be static and I tried printing duration.length in my while loop but it gives me an error saying that I can't make a static reference to a non-static field.
There was so much talking about regex and split, that I was uncertain what to do with the lines-to-be-saved. So I mentioned it savely 'splitAndSave', but I understand that no splitting is needed, that makes it easier. Sorry for the confusion. You don't now need a separate method. If you have an ArrayList<String> linesToSave, you can simply add each line to it when 'afterDuration' is true, instead of calling that method.
Post by:autobot
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, the permaculture playing cards