Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with boolean value being changed without being told to

    Hello everyone!
    I'm currently doing an assignment which involves a snack machine. I'm currently working on the PacketOfCrisp class. It needs to start with 10 crisps, and have a boolean field for whether or not it's closed. This field is called open and is obviously set to false when the object is created. It also needs to give out some messages, ie if the packet is not open then a message should appear saying that the packet is closed. If the pack has no crisps left a message should appear saying as such. Other than that when selecting eatCrisp the amount of crisps in the packet should go down by one. Here is what I have so far:

    Code:
    public void eatCrisp() 
    { 
    if(open = false) { 
    System.out.println("Open packet"); 
    } 
    else if (open = true && numOfCrisps >= 1) { 
    numOfCrisps = numOfCrisps - 1; 
    } 
    else { 
    System.out.println("No Crisps!"); 
    } 
    }
    Now for some reason when I invoke eatCrisp it takes out a crisp and changes the "open" field of the object to true and I can't figure out why. As far as I can figure it should be giving me a message of "Open packet" until I open the packet with the "openPacket" method, but instead "eatCrisp" is doing that by itself.

    Any ideas?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You fell into the same trap EVERY Java (and C and C++ and C# and JavaScript and...) programmer falls into. Often many times.

    Code:
    if(open = false) ...
    else if (open = true ...
    Now, try RECODING that as
    Code:
    if(open == false) ...
    else if (open == true ...
    The = operator *ALWAYS* does *ASSIGNMENT*. No matter where it is used. Yes, even in if and while statements.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    p.s.: The answer is exactly the same in JavaScript.

    And the answer you got there was correct. The person was trying to make you figure out the difference between = and == for yourself.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aaaaaah, thanks very very much! Really really appreciated!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •