Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

problem with logical condition

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to set up an if statement that has a logical condition where two different objects are compared for compatible mating based on their sex. So one needs to be a male and the other female to work. I have set up the genders by using a boolean checking for isMale to be true or not.

Every way I try to set up the condition, I cant get it right.

Here is what I have along with the entire method selecting the objects at random from an arraylist then getting the isMale value stored in each object.



Thanks a lot for the help
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

int mouse1 = random.nextInt(mice.length());



Can you exlain about this?

What is random?

if (mouse1Sex == true | mouse2Sex == false)



Why are you using "|" instead of "||"? Is it mandetory?
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I see in the first if-instruction: you probably meant to use the or-operator ("||") there, but wrote "|". But I think you want to really express something else, in words: "IF mouse1 is male AND mouse2 is NOT male THEN...". You can write that expression very clean by using the getIsMale() methods and the not-operator ("!").
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use "== true" or "== false"; simply leave out "== true", and replace "== false" with "!":
I also agree about not needing | - that will evaluate mouse2Sex even if mouse1Sex is already false. Use || unless you really need to evaluate both operands (which is rarely the case).
 
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the exclusive-or operator ^? Surely that situation is exactly what it is designed for?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just feel bad for all the homosexual mice :(
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the pointers, I will definetly use the ! to express the true and false conditions instead.

If it was just a simple matter of mouse1 must be a male and mouse2 must be a female to work, then I could put this together. My hang up is that one of the mice just be male and the other must be female. There is no condition that a certain mouse must be a male. I pick two mice at random then check to make one of them is a male and the other is a female, doesnt matter which order. I cant figure out a conditional statement that would make this work.

This may sound absurd but maybe a switch?



The only problem with this idea is mate() will always occur unless I put conditional statement in front of it which what I am trying to solve in the first place.

Any suggestions?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reminder: you're only checking that their genders are different, yes?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, is there a way to check for two different boolean values not matching?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That simplifies things somewhat, no?
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ben Hultin wrote:yes, is there a way to check for two different boolean values not matching?



if mouse1sex != mouse2sex ??
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sure, give it away :p
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh doi! That was too obvious, here I was trying to make a simple problem hard... I should be a bureaucrat :P

Thanks a lot for help, I will certainly remember this blow to my ego.
 
Campbell Ritchie
Marshal
Posts: 69779
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can use booleans after case in a switch block.
The != operator will give a very similar test to the ^ operator I mentioned earlier, but == and != have wider applicability.
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally, i think "mouse2Sex" is a bad name for the variable. If I just saw that in the code, I would expect it to hold something like "male" or "female", not "true" or "false". even if I saw it in a condition like "if (mouse2Sex)...", it's not clear what "true" means without digging through the code to find how/where it's set.

I'd suggest something like "isMouse2Male". seeing a variable named that, I'd expect it to be true or false, and I'd know what the values mean.
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic