Forums Register Login

String split

+Pie Number of slices to send: Send
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:



Any help is much appreciated!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Welcome to the Ranch

You may find it easier to use the overloaded version of String#split() which allows you to specify how many pieces you gt.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Exactly what I meant.
+Pie Number of slices to send: Send
 

Norm Radder wrote:

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?
+Pie Number of slices to send: Send
 

how would I store that in a String?


reg exp are normally Strings.  

Are you asking what the reg exp would be for the String you are searching for?
+Pie Number of slices to send: Send
 

Norm Radder wrote:

how would I store that in a String?


reg exp are normally Strings.  

Are you asking what the reg exp would be for the String you are searching for?



Yes, I'm not sure if the one I had in my initial post would work?
This is what I came up with, but I don't think it's doing what it is supposed to do?
 
+Pie Number of slices to send: Send
 

what it is supposed to do?


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.
+Pie Number of slices to send: Send
 

Norm Radder wrote:

what it is supposed to do?


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?
+Pie Number of slices to send: Send
 

asking whether you think it seems correct.  


Try it in a few simple tests to see if it works.


store the regex in a variable and then use it


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
+Pie Number of slices to send: Send
 

Norm Radder wrote:

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


The one I have is the one i posted in my first post: Duration \\s.+\\n
What do you think? Thanks!
+Pie Number of slices to send: Send
 

What do you think?  


Does it work?  Did you test it?
What are the rules for the String it is supposed to match?

A simple way to test:
+Pie Number of slices to send: Send
But must you use a regex? Can't you use String.contains or String.startsWith?
+Pie Number of slices to send: Send
 

Norm Radder wrote:

What do you think?  


Does it work?  Did you test it?
What are the rules for the String it is supposed to match?

A simple way to test:



Yes, it does work, but I did some tests and for some reason the code inside my while loop is not even being executed, I can't see why though.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

Norm Radder wrote:

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

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.  


Nope.  If the API doesn't say it takes a regex, don't try to use a regex.
This is exactly the same as
+Pie Number of slices to send: Send
 

Trent Green wrote:

 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?


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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

if my regex is correct


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.
+Pie Number of slices to send: Send
 

Knute Snortum wrote:

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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

I did test it and it works


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.
+Pie Number of slices to send: Send
In my first reply I hinted to this:

I don't understand why this simple code wouldn't at least be worth a try.
+Pie Number of slices to send: Send
 

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?



+Pie Number of slices to send: Send
 

I basically know my regex works


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
+Pie Number of slices to send: Send
 

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"?
+Pie Number of slices to send: Send
 

Knute Snortum wrote:

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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
 

Trent Green wrote:

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?



I meant:
+Pie Number of slices to send: Send
The ArrayList variable: duration needs to be defined outside of the method so that it will be preserved from one call to the next.
+Pie Number of slices to send: Send
 

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.
1
+Pie Number of slices to send: Send
 

gives me an error


Please post the code so we can see what is happening.

Only the main method should be static.  The other methods and fields should belong to an instance of the class and not be static.

If you do use static then everything must be static: methods and variables.
2
+Pie Number of slices to send: Send
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.
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1059 times.
Similar Threads
Array out of bounds error for unknown reason
how to find String array length
Text files - setting my own record separator
split method in string
BufferedReader.readLine() works in debug, but not run...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:48:33.