File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes comparing lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing lists" Watch "comparing lists" New topic
Author

comparing lists

Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Hey Java Gurus,

I am trying to write a method that takes two strings and compares them to existing reservations. The reservation object has a room number variable. The Suite also has a room number variable.

If the user passes in a string that matches an existing reservation, the method causes a 500 error message. If the user passes in a string that has no matches, instead of returning all the suites as available, it returns none.


I know this is very clunky code. I think it will eventually make sense to create an associate between a reservation and suite. Any thoughts on why it causes either error message would be greatly appreciated.

Thanks so much for your help,
Adam


Just Another Guy Hooked on Java
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A 500 will almost certainly have more information in the log file, assuming you have logging turned on.

I can't really read that logic condition--consider refactoring. I'm pretty sure you don't need to check checkin == checkout *and* checkout == checkin, though.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Adam Confino wrote:




Should not this code throw concurrentmodification Exception, if you pass some match. 500 message is of web page. That some exception was allowed to propagate back till response.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
some other considerations are that your suiteList and suitesAvailable is pointing to same object, so when you say a fresh list, you should explain, what do you mean? and print suitesAvailable before and after modification in case of no match just to make sure suitesAvailable has data intially and later it is not showing anything.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
Adam Confino wrote:




this peice of code may throw nullPointerException as well, incase suitesNotAvailable is empty.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3934
    
  17

Rahul.p Kumar wrote:
Adam Confino wrote:




this peice of code may throw nullPointerException as well, incase suitesNotAvailable is empty.


No, if suitesNotAvailable is empty the contains() method will always return false. There are two ways the code could throw a null pointer exception:
1) If suitesAvailable is null. We know suitesNotAvailable is not null because of the assignment to a new ArrayList with no possibility of re-assignment. But suitesAvailable may be null if suiteService.getSuiteList() is allowed to return null.

2) If suite was null, and suitesNotAvailable did not allow null values. As an implementation detail, suitesNotAvailable is an ArrayList and ArrayLists allow null, so this case would not happen with the provided code.

I do think the most likely source of the 500 error comes from a ConcurrentModificationException, but reading the logs is a better idea than guessing. You also might consider using suitesAvailable.removeAll(suitesNotAvailable) to achieve the task. Shorter code relying on the API implementations are usually more robust when you have the option.


Steve
Adam Confino
Ranch Hand

Joined: Sep 03, 2009
Posts: 48
Hey thanks a bunch. I'm going to try each of your suggestions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing lists
 
Similar Threads
Simple operator question
simple date format woes
dates, strings, and confusion
NullPointerException
checking for a range of dates