my dog learned polymorphism*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Null or Space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Null or Space" Watch "Null or Space" New topic
Author

Null or Space

Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
In the specification it says that:

All text values, and all fields (which are text only), contain only 8 bit characters, null terminated if less than the maximum length for the field


The problem is that this is not quite true. Having looked at the contents of my file the terminating characters they have chosen is not 0 (null) but 32 (space).

Question is when we write fields into the data file ourselves should we do what they say or do what they do?


SCJP 1.4
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Originally posted by Keith Jones:
In the specification it says that:



The problem is that this is not quite true. Having looked at the contents of my file the terminating characters they have chosen is not 0 (null) but 32 (space).

Question is when we write fields into the data file ourselves should we do what they say or do what they do?


I think that this requirement is there to test how you would deal with a situation where the customer specification is obviously wrong (They don't always know what they want).

I known some people have written back nulls when updating a file as per the spec rather than spaces.

Of course when reading in a record it doesn't matter because if you use String.trim() it will get rid of all charachters at the beginning and end of the string whose ascii value is less than the space charchter. (a space is value 32 and null 0). Also trimmed as are any other whitespace charachters in between such as carraige return, newline and tab).

It says in the requirements that other applications still use the same flat file format (which is why we have to implement the stupid DBMain interface) so I would be inclined to pad with spaces as this is what the other legacy applications would expect and padding the file with null may cause them to fail.

Regards,
Mark
[ November 22, 2006: Message edited by: Mark Smyth ]

SCJP<br />SCJD
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Thanks Mark,

that was a very clear and well argued answer.

Keith
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Whatever you decide, as always, document it.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null or Space
 
Similar Threads
Validating the Data File
Read and Write doubts
NX: null terminated if less than the maximum length for the field
B&S database file format
Null terminated strings?