• Post Reply Bookmark Topic Watch Topic
  • New Topic

String split  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Master Rancher
Posts: 2538
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 2538
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly what I meant.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Piet Souris
Master Rancher
Posts: 2538
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But must you use a regex? Can't you use String.contains or String.startsWith?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Piet Souris
Master Rancher
Posts: 2538
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my first reply I hinted to this:

I don't understand why this simple code wouldn't at least be worth a try.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?



 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Knute Snortum
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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"?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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:
 
Norm Radder
Rancher
Posts: 2943
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ArrayList variable: duration needs to be defined outside of the method so that it will be preserved from one call to the next.
 
Trent Green
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Norm Radder
Rancher
Posts: 2943
32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Piet Souris
Master Rancher
Posts: 2538
87
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!