aspose file tools*
The moose likes Java in General and the fly likes Hopefully my last question for today... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hopefully my last question for today..." Watch "Hopefully my last question for today..." New topic
Author

Hopefully my last question for today...

Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65


I wrote this incorrectly so that when someone invokes the rent out method, it will still add one to the times rented field even if the movie is rented out.

So I need to change it so that if currentlyRented already = true, it won't do anything. But I am not sure how I would make this happen.

I would like it to do nothing if it is already true, and if it is false, chget it to true and add one to times rented.

But how do I set something in a method to do nothing?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Lisa,
Note that "if (currentlyRented = true") sets it to true, while
"if (currentlyRented == true)" actually compares the two values.
"if (currentyRented)" does the same.

Now on to your question: There are two ways of doing nothing. Option 1 is to just add a comment. You could just have an empty if block, but that is confusing.


Option 2 is to rewrite the if statement to avoid the "do nothing part":


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Hi Lisa,

To do this, you should replace:

???do nothing;

in your code with:

return;

This way, if the movie is currently rented, then the method will just return without going any further.

Hope that helped. If anything above was unclear to you just give me a shout.

Cheers


SCJP J2SE 1.4<br />SCBCD J2EE 1.3
Lisa Beglaw
Ranch Hand

Joined: Jul 16, 2005
Posts: 65
Jeanne - Again with the = vs ==. This is the second time today I have been caught on that one! Thank you!!!

As for the rest, thank both.

Jeanne - I really like your shortened version and will give that a try.

M Jay - if I have the word 'return' at the end...will it actually return something?

I am off to test both! Thank again!
M Jay
Ranch Hand

Joined: Sep 21, 2004
Posts: 66
Dearest Lisa,

Because your method has a return type of VOID, it cannot return anything. So return in such situation means "do nothing and finish executing this method".

Hope that was helpful.

Cheers
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Personally I would prefer Jeanne's version. In some circles using a return statement other than at the end of a method (which isn't even needed in a void method) is considered bad practise because it can make it confusing to understand what the method is doing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hopefully my last question for today...