• Post Reply Bookmark Topic Watch Topic
  • New Topic

alternative to nested if in TreeMap values  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:


The above seems a long winded way of going about checking whether any of the values are null or empty strings (especially if you have thousands of values).........

Is there an easier way with less code to check the values in a TreeMap being null or empty?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roger,

Many thanks for your reply; much appreciated.
Will have to look into this as not come across this as yet.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have looked in the following books:

Java 2 Sun Certified Programmer & Developer for Java 2
by McGrawHill Osborne

Complete Java 2 Certification fourth Edition Philip Heller Simon Robers

A Programmers Guide to Java Certification Second Edition
Khalid A. Mughal and Rolf W.Rasmussen

And unfortunately had very little understanding of subclass TreeMap, overriding the put( ) method to disallow null or empty values.

My course notes didn't even go there.

Could anyone suggest another place where I would be able to delve deeper into this?

Thanking you in advance for your time.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And unfortunately had very little understanding of subclass TreeMap, overriding the put( ) method to disallow null or empty values

Hmm. There seems to be a missing post so I'm not sure if I quite understand this - so sorry if I'm off on the wrong track, but this is how you might do what you mention:

But I don't really see how this addresses your issue - your checking code would still have to use conditionals to do its work.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just trying to clean up all the else-ifs in there ... Read up on exceptions, maybe in Thinking In Java and see if that's an appropriate way to control your program:

To remove the elses and make your message even more informative:

You could use the exception with the improved message, too. Let me know if those ideas work for you.
[ May 04, 2005: Message edited by: Stan James ]
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MMmmm - I think I'm sounding like I'm making this more difficult for myself (and probably confusing myself and everyone else). I will try and elaborate with more information.

I have a class Vehicle Record (this has loads of exceptions i.e. try and catch and when tested on it's own works like a dream. No problems here I feel.

I then have another class VehicleVB. This has a method store, that has the following parameters and declarations which should not be changed.



In the constructor of the class VehicleVB I have:



So I go to my store method to store the VehicleRecord vr BUT I wish to exclude all values which are null or have a string where it is empty.

I find I have no problems with this when using millions of if statements to go through each of the Strings in VehicleRecord vr i.e.



But I thought this was madness as what if you had a database with a thousand values to one key - it would be alot of if statements. So I wondered if there was an easier way?

I then took the approach that if dbase values were equal to null or an empty String then I could just delete it after it was stored maybe?

So I tried:


But got the following error at run time
Exception in thread "main" java.lang.NullPointerException
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.getEntry(Unknown Source)
at java.util.TreeMap.remove(Unknown Source)
at VehicleDB.store(VehicleDB.java:121)
at TestApp.main(TestApp.java:365)

I then tried:

But got the following error at run time:
Validating Registration Date: 0182
The length of the date input by user is: 4
Length of date successfully validated i.e. four digits or characters
VALIDATING YEAR
newYear is: 1982
Finished validating Registration
VALIDATING MONTH
newDate is: 01
Registration Month successfully validated
VALIDATING REGISTRATION NUMBER: ZZ43SSX
The length of the reg input by user is: 7
Length of registration: VALID i.e is 7 characters

VALIDATING FORMAT OF REGISTRATION:
First Character is a letter
3rd and 4th characters are a Digit
5th, 6th or 7th character is a Letter
2nd character is a letter or a number
VALID REGISTRATION FORMAT
Zero null values AND Zero empty strings

The Collection consists of the following:
[NV52SSD=NV52SSD 1003 Ford Mondeo White, QP43SSX=qp43SSX
3 Ford Fiesta Pink, ZZ43SSX=ZZ43SSX 0182 Ford Fiesta
Yellow]
Number of records in database is: = 3

Validating Registration Date: 0285
The length of the date input by user is: 4
Length of date successfully validated i.e. four digits or characters
VALIDATING YEAR
newYear is: 1985
Finished validating Registration
VALIDATING MONTH
newDate is: 02
Registration Month successfully validated
VALIDATING REGISTRATION NUMBER: null

The Collection consists of the following:
[NV52SSD=NV52SSD 1003 Ford Mondeo White, QP43SSX=qp43SSX
3 Ford Fiesta Pink, ZZ43SSX=ZZ43SSX 0182 Ford Fiesta
Yellow]
Number of records in database is: = 3

Finished!!!
Exception in thread "main" java.lang.NullPointerException
at VehicleRecord.validateReg(VehicleRecord.java:391)
at VehicleDB.store(VehicleDB.java:115)
at TestApp.main(TestApp.java:710)

So I decided to look at subMap views (as detailed in http://math.hws.edu/javanotes/c12/s3.html) BUT this comes across as though you can search on the key but not the values associated with the key.

Am I making things too complicated for myself unnecisarily?
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just had a thought.......... Should I just ensure that null values and empty Strings should not be permitted in the VechicleRecord class? And therefore NOT leave it to the TreeMap in the VehicleDB class?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good thought, but it depends. Can a VehicalRecord have a valid reason for containing null values? I suspect that the answer is yes. These nulls only become invalid when you add them to the "database", yes? I'd forget about the TreeMap - think of it as just a place-holder. Validate your data first, if its valid then adding it to this place-holder is a no-brainer.

Re-read Stan's post, he's pointing you in the right direction.
[ May 04, 2005: Message edited by: Paul Sturrock ]
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for your response. Much appreciated!

I feel, even looking at Stan's post it's an awful lot of coding. I have the following and only have five values associated with the key registration.

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no reason to try and catch and display. When I suggested exceptions I thought the method would just let the exception go up to whoever called it. If you cannot do that, the display is sufficient.

You're probably right to be troubled by excessively long validation code. Maybe that code is hinting that it would like to be in its own class or pushed off to a validation framework. A quick google on "java validation framework" got many hits.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

Many thanks for your response and your time.

I've gone with the following code as it works.



My test program consisted of the following:



And my output for the code was expected. (I've only displayed the last few lines as the others would take up pages!)

null value not permitted for Reg. This record will not be stored.
Reg has an empty String value. This record will not be stored.
Reg Date has an empty String value. This record will not be stored.
null value not permitted for Reg Date. This record will not be stored.
Car Make has an empty String value. This record will not be stored.
null value not permitted for Car Make. This record will not be stored.
Car Model has an empty String value. This record will not be stored.
null value not permitted for Car Model. This record will not be stored.
Car Colour has an empty String value. This record will not be stored.
null value not permitted for Car Colour. This record will not be stored.

Finished!!!

I still feel there MUST be a way with less code?

I am concerned that to write code for an if statement for every field in a database to see whether it contains an empty String value "" or null is a bit cumbersome. What happens when you have thousands and thousands of fields?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a couple of comments. First of all, what is InvalidRecord? Is this a custom exception class? If so, it is customary to end exceptions with the word Exception. You should consider changing this to InvalidRecordException.

Next, exceptions are usually used to signal OTHER methods that an error occured. This means that you typically don't throw and catch exceptions within the same method. Instead, you allow them to propogate back to the method that called it so the error can be dealt with appropriately. I strongly suggest you read up some more on exception handling to get a better idea of how exceptions are intended to be used.

Finally, are there some of these particular values that should not even be allowed in the VehicleRecord class to start with? It seems like you can simplify this bit of code by deciding what are valid values in VehicleRecord. For example, if you have a setColour() method, should the caller be allowed to pass "" as the parameter? If not, then the error handling for this particular attribute should be done in the setColour() method. In contrast, it might make sense for the VehicleRecord class to allo setReg() to take a null value.

In short, you should decide what values are valid for the VehicleRecord class to have in the first place. The main idea here is to do the validation as early as possible. Then if you decide that particular values are valid in the VehicleRecord class but are not allowed in the TreeMap, you can deal with them later as you are doing here.

I hope this helps. Let us know if you have any more problems.

Regards,

Layne

This has been mentioned before, but I thought I'd bring it up again. Which of these values are valid for the VehicleRecord class? It looks like in some cases that you are throwing an InvalidRecord
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Layne,

Many thanks for your response and time. Much appreciated.

InvalidRecord is an Exception class but we have been requested to call it InvalidRecord. (Thankyou for pointing this out though; I had read that it was the norm to include Exception in the name).

The VehicleRecord class is to permit empty Strings i.e "" and null values.

The VehicleDB class is NOT to allow empty Strings or null values.
(It would of been so much easier to put these in the set methods in the VehicleRecord class).

Again, many, many thanks for your time. Especially when it's so late/early in the day.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!