• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Simple operator question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Marshal
Posts: 76822
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
     
    author
    Posts: 23928
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Adam Confino
    Ranch Hand
    Posts: 48
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 1183
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 48
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Sebastian for clarifying that. Anyone know why this method returns either all true or all false?

    Thanks.
    Adam
     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 . . .
     
    Sheriff
    Posts: 22700
    129
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't know what reservation.getCheckIn() returns, but you may want to consider caching its value in a local variable.
     
    Campbell Ritchie
    Marshal
    Posts: 76822
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 48
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic