• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Find the Beginging Position of a Field With Pipe Delimeter

 
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Guys This file of Mine Is giving Me trouble Because I now need to find the Beginning Position for each field:

The function



Returns the Position as long as the Field is not repeated, But My file has Repeated Fields And I need To Know the Beginging Position of each. My file is:



What is the best way to get the Beginning Position of each Field

 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two possibilities:

1. The position of the first field is 0. The position of the second field is the postion of the first field + the length of the first field, the position of the third field is the postion of the second field + the length of the second field, etc

2. There's an overloaded indexOf method that takes 2 parameters - see if you can work out how that might be useful.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adrian , Your Option 1 is correct. The problem is how to capture that Dynamically in a file that has over 400 fields.

Here is part of the Code:
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create an int variable that holds the current position, initialise it to 0, then each time round the loop you add the length of the current field to this variable. The start position of the next field is then the current value of this variable.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Adrian Burkett.

Stanley Mungai, this question seems so similar to your earlier thread that I am closing it. Please continue all discussion there.

You appear not to have read what it says about the pipe character; you have already been told how to change that. I shall make more comments on the other thread.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All, right Stanley Mungai, you have convinced me I ought not to have closed this discussion. I still think it might have followed better from your old thread, though.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another approach: Rather than use String.split(), use java.util.regex.Pattern and Matcher. Matcher has methods that will let you find the positions of your matches.

It seems kind of pointless to first find the delimiters with split(), and then find them again with indexOf(). Of course, in this case, you could just use indexOf() for all of it and skip split() completely. However, it's good to be aware of Matcher for when you have a more complex case.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch Adrian Burkett.


Muchas gracias mi amigo
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using Jeff"s idea Of Regex I came up with the Following Code that has Succeeded in Displaying the Position for the First Four Records, But If I change the Integer at Line 37 and Line 38 to something else, The code Does Not return Anything. What could be the Issue this time? IS it the Regex I am Using or what?

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Using Jeff"s idea Of Regex I came up with the Following Code that has Succeeded in Displaying the Position for the First Four Records, But If I change the Integer at Line 37 and Line 38 to something else, The code Does Not return Anything. What could be the Issue this time? IS it the Regex I am Using or what?



You changed the integer on line 37 and 38 but you didn't change your input, right? So when the (magic) number is 4, it works because that's how many groups your Regex expects and that's how many groups your input has. Same thing will probably happen if you change just your input to have more columns; I'm guessing (because I haven't actually tried to run it) that the regex will not match the input anymore . Your regex expression is also very specific to input having exactly 4 fields. Change it so that it can handle any number of columns. Then all you have to do is set up a loop that iterates over however many groups (columns) your regex finds.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Using Jeff"s idea Of Regex I came up with the Following Code that has Succeeded in Displaying the Position for the First Four Records, But If I change the Integer at Line 37 and Line 38 to something else, The code Does Not return Anything. What could be the Issue this time? IS it the Regex I am Using or what?


Junilu's told you the basics, but the fact is that you're overthinking this.
Jeff suggested regexes, but he also went on to say that you can do it without them, which you can.

Regexes are nice, but they're not a panacaea; and they're slow (at least, slower than indexOf()).

Personally, I would use split(). You almost had a solution with your previous code.

But if you're worried about performance (which is the only reason I can think that you'd be worried about having 400 fields (which is nothing to a split())), my suggestion would be to use String.indexOf(char, int), which is likely to be quite a bit faster than any regex-based solution.

Winston
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I there fore need a regex that will take care of all the COlumnss not four. I cannot use that one for 400 fields!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:I there fore need a regex that will take care of all the COlumnss not four. I cannot use that one for 400 fields!


For the final time:
You don't need a regex any more complicated than "[|]"; and splitting a line into 400, 4,000 or even 40,000 fields is child's play for a split().

As I said before, if you're worried about performance, use indexOf(), which doesn't use regexes AT ALL.

Winston

 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried Index of But I got Wrong Output because some of the Fields are repeated.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, you could probably save yourself a lot of coding and grief if you used a java.util.Scanner instead.

A few comments about the code in your full listing (49 lines):

1. As shown, the indentation of the code is misleading. Bad indentation fools readers of your code, possibly including yourself, into thinking certain lines of code are grouped together when they aren't and vice versa. Properly indented code facilitates correct understanding of the program flow. Improperly indented code misleads the reader and is a breeding ground for bugs.

- Line 14 should be indented so that it's in line with Line 19
- Line 23 should be aligned with Line 22
- Line 24 is properly indented in relation to line 23 and should remain so after line 23 is realigned
- Lines 36 through 43 should be indented in relation to line 35
- The curly brace on line 44 appears to match line 19 but it shouldn't. It should match indentation with line 35
- The curly brace on line 47 doesn't appear to match anything; it should match line 23 (for int j)

2. There is no point for the declaration and assignment being done in line 26. The 'result' variable is never used in the code.

3. Likewise, the loop from line 28 to 30 is pointless. This loop will iterate exactly one time every single time. And all it does is read a line from the current file being processed and basically throws it away because Line 31 immediately reads in the next line.

However, most of this is moot if you change your approach and use a Scanner instead.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:I tried Index of But I got Wrong Output because some of the Fields are repeated.


OK, you're going to have to explain what you mean by "repeated". Perhaps you could show us an example.

All line.split("[|]") does is break up the String line into "fields", where each "field" contains the data between (but not including) the '|'s.
The only caveat is that trailing empty (== 0-length) "fields" are not included.

If n is the number of '|'s in line, and you always want the array to contain n+1 entries, use:
line.split("[|]", Integer.MAX_VALUE)

(all this is in, or can be gathered from, the documentation).

Once you have your array of fields, you can then decide what you want to do with "repeated" data.

Winston
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston: Here Is you version of Idea :




This Code Ok Displays the Position of Each Field But How do I Integrate My for Loop With the Previous code: This One:


 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stanley,

I already commented about this before but I'm really curious to know what you want to achieve with this part of your code:


The statement on Line 1 seems pointless since result does not appear to be used anywhere else and the for loop on Line 3 is also pointless. Actually Lines 3-5 are pointless because they are not executed at all. You initialize i to 0 and the termination condition of the loop is (i < 0) which makes it terminate even before it has a chance to be executed.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lacar That for Loop reads the First Line of the file as that is the only Line I am Interested in. The String result is used in the file name of the text file Am writing Do not worry about those as this is just part of the code. Am Interested In Integration the two bits of code into one Program As Currently I am Runninf it as two Program
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Lacar That for Loop reads the First Line of the file . . .

Nonsense! That loop does nothing but use a few CPU cycles.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Winston: Here Is you version of Idea...


No. My version of the idea would be something like:except that I'd probably break it up into separate methods.

You might want to think about a Field class as well.

Winston
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you really mean for, Winston? Shouldn’t you have a few more semicolons, or change to while?
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nic e idea Winston Except for that you code line 05 returns an Error ("Not a Statement"). Is that Not Supposed to be a while Loop? Secondly, I am Not Interested in the Entire File, I just want the First Line of the File And That was Why I had the For Loop In My code number 06. The Positioning Displayed will be wrong also because we should add the Positions occupied by the delimiters in the Previous fields.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Do you really mean for, Winston? Shouldn’t you have a few more semicolons, or change to while?


Quite right. That's what comes from copying someone else's code and NOT TESTING.

I've changed the offending line and left the other for posterity.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:The Positioning Displayed will be wrong also because we should add the Positions occupied by the delimiters in the Previous fields.


Quite right too. Sheesh, I'm batting a thousand this morning. Time for another coffee...

Winston
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Lacar That for Loop reads the First Line of the file as that is the only Line I am Interested in. The String result is used in the file name of the text file Am writing Do not worry about those as this is just part of the code. Am Interested In Integration the two bits of code into one Program As Currently I am Runninf it as two Program



Oh well, I tried to show you a good spot to throw your line in but looks like you've been asking for a fish all along... (shrug) Good luck.
 
Stanley Mungai
Ranch Hand
Posts: 155
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lacar, there is nothing I have Not tried. No, I am not Looking for a Fish I implemented Your Idea this way:




The Problem is on on Line 35 of the Code because if A field For example Does Not Contain Any Characters but has a Length, it is returning Zero For that field. For Example: it should return size 60 for that Field but it returns Zero.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stanley Mungai wrote:Lacar, there is nothing I have Not tried. No, I am not Looking for a Fish I implemented Your Idea this way...


I don't understand what your problem is any more. You've been given several suggestions; I even went against my usual policy and provided you with code (allbeit slgihtly flawed, as you pointed out ).

Both String.split() and String.indexOf() will do what you want, and there's absolutely no need for a regex like your

Pattern.compile("\\s*(.*?)\\s*\\|")

It's a sledgehammer to crack a walnut.

Winston

[EDIT] PS: I split up that very long line (35) in your last code. Try to avoid them when you're pasting in code blocks because they tend to screw up the windowing here.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic