aspose file tools*
The moose likes Java in General and the fly likes To use break statements or not to use break statements, that is the question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "To use break statements or not to use break statements, that is the question" Watch "To use break statements or not to use break statements, that is the question" New topic
Author

To use break statements or not to use break statements, that is the question

Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65


The only part of this that isn't working the way I want is the '==null' part. If I try to rent a movie that ISN'T in my collection, I want the boolean value to return as false and I want a message displayed that says the movie isn't in the collecion.

With the break statements:
The null method doesn't ever get used, if I try to return a movie that doesn't exist I get the false return plus the message "Selected movie is not currently rented out, cannot return."

Without the break statments:
If I try to return a movie that isn't in the collection I get the false return plus both messages, "Selected movie is not currently rented out, cannot return." and "Selected movie is not in the collection."

How can I break this up so they both get the return of false, but each one has a differnt message?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42944
    
  68
Firstly, the comparison with null should be the first of the three, because if it is true (i.e. the object is null), either of the two other statements would bomb out with a NullPointerException. So currently, that comparison is never going to execute.

Secondly, with the code as shown you can never get both message for the same movie, because only one of the three branches ever gets executed. You can only get them in succession for two different movie titles if the first branch is taken (because that is the only one that does not terminate the iteration).

It is not totally clear to me what the code is supposed to do, so I can't comment on the iteration logic. E.g. how do you know which movie is to be returned given that the 'title' parameter is not used?
[ August 07, 2005: Message edited by: Ulf Dittmer ]
Dale Seng
Ranch Hand

Joined: Mar 22, 2004
Posts: 275
I'm guessing at what you want to do, but I think I have a better way to proceed. You might create a map object and use that, instead of looping to find what you need.

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Also notice that the method as written *always* will return false (even if you drop the break statements).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To use break statements or not to use break statements, that is the question