
-----------------------------------
nelsonkkyy
Wed Oct 19, 2011 7:22 pm

boolean expression problem!
-----------------------------------

import java.util.*;

public class LockTester
{
    public static void main (String






public class Lock
{

    //instance variables
    char word;
    char password;
    boolean status;

    public Lock(char x ,char y ,char z )
    {
    
        password = (char) (x + y + z);
       
    }
 
    public void set(char x)
    {
      

     word = (char)(x); 
    
        .
    }

    public void pull()
    {

         if (word == password)
         status = true;

    }

    public boolean isOpen()
    {


    if (status = true)
                        {System.out.println ("true") ;}
                        else
                        {System.out.println ("false") ;}
                
                return status;
        
    }

    public void close()

       {
        status = false ;
        }
}
/////////////////////// the out put is always true when the word is not even equal to password, how do i make it prints false when the word is not equal to password????///////////////////
[/quote]

-----------------------------------
Tony
Wed Oct 19, 2011 7:30 pm

RE:boolean expression problem!
-----------------------------------
1) you already have a question thread for this

2) because the value of "status" is always "true" at the time you print anything.

-----------------------------------
Zren
Thu Oct 20, 2011 12:55 am

RE:boolean expression problem!
-----------------------------------
= (assignment) is not the same as == (operatator). And yes, you can assign variables inside if statements legally (and lots of other places).

These are equivalent (a is a boolean).
if (a == true) {}
if (a) {}

Assignment inside if statement. The assignment will return the variable being assigned.
if ((a = true) == true) {}
So it'll turn into this afterwards.
if (a == true) {}

So since you had:
if (a = true) {}

It returned:
if (a) {}

Which since you just assigned as true.
