aspose file tools*
The moose likes Beginning Java and the fly likes Simple operator question 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 "Simple operator question" Watch "Simple operator question" New topic
Author

Simple operator question

Adam Confino
Ranch Hand

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

I want this logic to read "if reservation.getCheckIn is after stringCheckIn AND before stringCheckOut, return true".

Is my code correct?

Thanks a bunch for your time
Adam


Just Another Guy Hooked on Java
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Use a short-curcuit operator. So instead of &, use &&.

Other than that it's fine, you could even get rid of the if statement by simply doing this:



JDBCSupport - An easy to use, light-weight JDBC framework -
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Yes and no.
Assuming there are appropriate methods in your reservation object and its checkIn field, the latter taking a String as a parameter, and both returning booleans, then that code will work. But it won't return false.

There are two ways to improve it.
  • Replace the & operator by &&. It is usual to use && for boolean AND-ing. Remember the right-hand operation will only be executed if the left-hand operation returns true.
  • Get rid of the if. That improves the style.
  • You would then writeRemember that a "return" terminates your method, so you cannot put code after a "return" statement. Note you have no need to write "return false;" anywhere.

    The bit about losing "if" matches these style conventions (section 10.5.2).
    Henry Wong
    author
    Sheriff

    Joined: Sep 28, 2004
    Posts: 18104
        
      39

    Sebastian Janisch wrote:
    Other than that it's fine, you could even get rid of the if statement by simply doing this:


    Be careful here. This is not a direct substitution. In the first example, only true is returned, the next line (following the if) is executed if the expression is false. In the second example, the value of the condition is returned, regardless of whether it is true or false.

    Henry


    Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
    Adam Confino
    Ranch Hand

    Joined: Sep 03, 2009
    Posts: 48
    Hey guys,

    Perhaps I should have included more code...I try not to write too long of posts.

    My real issue is that this method returns true in all cases or false in all cases, but never a combination. I think I have a logic error. Any suggestions would be appreciated. Thanks so much.

    Sebastian Janisch
    Ranch Hand

    Joined: Feb 23, 2009
    Posts: 1183
    You want to use && instead of &.

    Think about it. If you use AND it means both ends of the expression need to be true, if the first one is already false, there is no need to check the second condition, hence && makes more sense.

    & on the other hand will check the second expression no matter if the first was already false. This is only useful if your method call causes any side effects that you wish to cause no matter of the expression is true or false.
    Adam Confino
    Ranch Hand

    Joined: Sep 03, 2009
    Posts: 48
    Thanks Sebastian for clarifying that. Anyone know why this method returns either all true or all false?

    Thanks.
    Adam
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    You are only ever checking the first true. You start off with a list of reservations, go through them, find something out about them, return from the method . . .

    The first true will return, and that terminates the method. If you hunt for our JavaRanch style guide, which can be found from our title page by clicking "more" it says that using return in the middle of a method can be confusing. That is why. You could reduce your boolean expression to a single statementIf you can actually read that statement!

    Can you make a boolean[] or List<Boolean> which you can then return with all the reservations' date-checking in?

    I did say earlier that you can't have anything after a "return", which is what Henry meant about not being able to test anything else afterwards. Maybe not a good idea to have side-effects in a boolean expression.

    Additional: code style suggestions here
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    Why don't you have a method in the Reservation class to check whether a date is within the requisite range?
    What will happen if you create a Reservation for 14th September to 16th September and pass check-in date 13th September to it? Check-in is before the termination date of your Reservation . . .
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19541
        
      16

    I don't know what reservation.getCheckIn() returns, but you may want to consider caching its value in a local variable.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 36478
        
      16
    Actually you might find it easier if you consider

    String check out >= String check in. OR: not earlier than check in.
    String check in <= date check out, OR: not later that check out date.
    String check out >= date check in, OR: not earlier than check in date.

    Then maybe you can reduce it to three tests.
    Adam Confino
    Ranch Hand

    Joined: Sep 03, 2009
    Posts: 48
    Hey thanks. I'm still working on it, but I'm going to chew an each of your suggestions. I appreciate you helping me think this through.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Simple operator question
     
    Similar Threads
    Object Control Creation – From Constructor?
    Java Mail Sending troubleshooting
    NullPointerException
    comparing lists
    checking for a range of dates