• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

nulls detected in string array not being saved by class constructor

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings all,

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, LLC||doug@pencil.com|||85637889|Cross, Ben|bcross@godaddy.net|Bernard|Cross|Ben||315 One Tree Hill Terrace|Lafayette|LA

62RG|fe|Pencil Financial Group, LLC||tracy@pencil.com|||13658734|Dustin Cardwright|dcart@motorola.com|Dustin|Cartwright|||| |LA


which I parse and store with


and my Contact class has the constructor


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?

TIA,

Still-learning Steve
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and the answer came to me as soon as I posted, of course, to use String'. isEmpty() as so


CASE CLOSED

 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
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

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.

Winston

PS:
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
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic