I've a vertical-bar-delimited file where most elements contain text, some contain whitespace, and some are empty. Examples:
62RG|fe|Pencil Financial Group, LLCemail@example.com|||85637889|Cross, Benfirstname.lastname@example.org|Bernard|Cross|Ben||315 One Tree Hill Terrace|Lafayette|LA
I expect the for-loop in the constructor to find any elements containing whitespace characters and set them to null for subsequent assignment.
And when the code runs I do see some hit-statements pop up, so the detecting part is working.
But when I then process the list and access a Contact object and test fields for nulls I don't find any ie
never prints when it should.
What's the trick? Or is my approach wrong and if so what should it be?
I'm not exactly a regexpert, but it looks to me like you're expecting an empty string to match an expression which is looking for exactly one whitespace character. But since 0 is not equal to 1, it doesn't. If you simply want to see if a String is empty, consider the "isEmpty()" method.
EDIT: While I was halfway through posting, I had to talk to my sister on the phone. And then when I came back I saw what I had typed was wrong, so I went back and typed the right answer. Which you had already figured out. As you say, Case Closed.
ANOTHER EDIT: Actually, maybe not. What happens if you have more than one whitespace character between delimiters? Or does that not occur in your data?
Stuart Rogers wrote:and the answer came to me as soon as I posted, of course, to use String'. isEmpty()
That's one possibility, but I wonder if you're not overthinking this.
First off: You haven't explained why you need to know this, nor indeed whether there is any logical difference between an empty String and one that contains some whitespace. Your "solution" would suggest not.
And if that's the case: why not just:or indeed, just do your original split with:
String str_arry = innline.split( "\\s*\\|\\s*", 17); (not sure why the '17', but I assume you have your reasons)
It may not be what you want, but TBH, it's difficult to know exactly what you do want right now.
1. Using underscores in names is not generally a good idea. To a Java programmer it looks 'C-ish'.
2. Not quite sure why you have duplicated letters in your names ('ii', 'innline', 'lisst'). It looks really weird.
3. I've removed the code block from your 'records' and used the 'tt' (true-type) block instead. Have a look at the DontWriteLongLines page to find out why.
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss