1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Why is my if statement executing when its false

  1. Sep 11, 2012 #1
    quick question, not sure why i cant work this out.
    in the following code my if statement executes, but as far as i can see i have set the boolean so that it shouldnt.
    Code (Text):

    public void actionPerformed(ActionEvent e)
                       boolean contains = false;
                       for (int i = 0; i < studentStrings.size(); i++)
                            if( studentStrings.get(i).contains( enterID.getText()))
                                System.out.println( " get constains executed");
                                contains = true;
                        if ( contains = true)
                            System.out.println( "if executed");
                            System.out.println( "else executed");
    you can see there are 2 ifs and one else. When and if the first one executes it should set the boolean contains to true. then if boolean contains is true my second if should execute and not my else. and if the first if doesnt execute the boolean should still be false so my else will execute. the system.print methods are just so i can see which ones are executing.

    in the for statement the idea is i look for a string in the arrayList that is entered into a student ID text field. if the ID already exists it updates that student, if not it creates a new record.

    the problem is whether or not that first if statement executes, only my second if executes, never the else.

    so if my jtextfield has an existing ID number executed i get

    get contains executed
    if executed

    and if it does not contain an existing ID then boolean should remain false and then only execute the else but i get

    if executed.

    i dont know why my if statement arguments arent working?

    1. The problem statement, all variables and given/known data

    2. Relevant equations

    3. The attempt at a solution
  2. jcsd
  3. Sep 11, 2012 #2


    User Avatar
    Homework Helper
    Gold Member

    You are missing an equals sign. If (contains = true) sets contains equal to true and then tests whether it is true. If (contains == true) just tests whether contains is true without changing its value.
  4. Sep 11, 2012 #3
    Code (Text):
    There is a difference between assignment and comparison.

    The expression of assigning a boolean can be treated as a boolean.

    Code (Text):

    boolean b = false;

    if( b = true) //expression in () sets b to true and evaluates to true
        System.out.println( "THIS LINE WOULD GET PRINTED");


    As an aside, one may write
    Code (Text):
    if(b == true)
    more concisely as
    Code (Text):
  5. Sep 11, 2012 #4
    Thanks, knew it would be simple!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook