aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Trim spaces? 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 "Trim spaces?" Watch "Trim spaces?" New topic
Author

Trim spaces?

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

When reading or writing records back to the data file did you guys trim the strings to remove whitespaces?


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5534
    
  13

When records are added to the record cache the field values are trimmed. If you write records back to the file, why would you trim them if you pad the field with spaces?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Cheers Roel. The specific case I was pondering about was what if someone sent a record in to my business class where the field was a string that exceeded the schema.

I too am trimming strings when I load the cache. I am also trimming strings when converting my transfer object to a String[] array.

In my setup, someone could set a field that was too long, but because of my trimming in converting a transfer object to a String[] array everything will be ok.

So, to answer your question of why you would trim when writing back, simply because someone could have set a field to a string that was too long, and trimming it would reduce the length (if the value happened to have whitespaces at the end).

I understand you will have prevented this from happening in your GUI. But I look at this from the point of view of someone using my business class.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5534
    
  13

Maybe you should look back to the title of this post and confirmed by the question in your 1st post: "did you guys trim the strings to remove whitespaces?", so I respond to what you ask. I can't know you actually meant trimming of values which are too long. I'm not a clairvoyant

In my application a String[] with a value which is too long, won't pass the validation. I believe Roberto trimmed strings which are too long for the field.
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 172

Sean Keane wrote:The specific case I was pondering about was what if someone sent a record in to my business class where the field was a string that exceeded the schema.

I validated input length in the GUI, trimmed whitespace (if any) coming out of the database, and padded to max length (if necessary) on the way into the database. I also truncated if necessary on the way into the database, on the theory that my data class shouldn't depend on a validation in this GUI in a project that anticipates the adoption of other front ends. I didn't use a cache, so I can't speak to that.

I understand you will have prevented this from happening in your GUI. But I look at this from the point of view of someone using my business class.

My reasoning was the same. I pad or trim as necessary. My earthbound classes shouldn't know what's happening in the upper atmosphere.


OCMJD 6, OCPJP7, Baroque Potion, G+
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Roel De Nijs wrote:Maybe you should look back to the title of this post and confirmed by the question in your 1st post: "did you guys trim the strings to remove whitespaces?", so I respond to what you ask. I can't know you actually meant trimming of values which are too long. I'm not a clairvoyant


I think you may have misunderstood me. But apologies if any confusion. My second question was a follow up one on the first question - I wasn't implying you didn't understand my first question .

Roel De Nijs wrote:In my application a String[] with a value which is too long, won't pass the validation. I believe Roberto trimmed strings which are too long for the field.


I am in two minds which approach to go with here. I originally went with trimming. But thinking of it in database terms, if I ask an API to write "abc " to the database, then I expect to get "abc " back from the database if I read it back after writing.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

David Byron wrote:I also truncated if necessary on the way into the database


I know this is probably a little on the theoretical side - but do you see the potential for confusion that I outlined in my previous post caused by trimming on the way in and out of the database?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5534
    
  13

Sean Keane wrote:I am in two minds which approach to go with here. I originally went with trimming.

I think it's situation dependent. For example: people can send through our application mailings to a selection of members in our database. Each sent mailing is logged in a table and we keep some description about to which members it was sent (everybody between 15 and 35 years, living in Belgium,...). Such description can get quiet long and although we have a column of 10000 chars, it could exceed this value, so here we trim the value. Because you certainly don't want a DataTruncationException being thrown and you also don't want a user be prevented to send a mailing, because some column's size is not big enough.
But if data is written to the database which can be changed by the user, it must fit in the appropriate column, otherwise an exception will be thrown.
David Byron
Rancher

Joined: Jan 20, 2009
Posts: 172

Sean Keane wrote:I know this is probably a little on the theoretical side - but do you see the potential for confusion that I outlined in my previous post caused by trimming on the way in and out of the database?
Sure. In general, I agree with Roel that it's situationally dependent.

Validations in the client and biz tiers should normally ensure that the need to truncate never arises. But at some point, writing to a 50-wide column is writing to a 50-wide column. The place to save the user from himself is in some higher layer. Below the realm of Recycle Bins, though, there's a place where rm -Rf is going to nuke your stuff, no questions asked.

I can see validating further even at the point of data access just to ensure that the bad case never occurs, but that's not the choice I made for this assignment.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trim spaces?