Why is my if statement executing when its false

In summary, the conversation discusses a problem with a code where the second "if" statement always executes instead of the "else" statement, even if the first "if" statement does not. The issue is due to the incorrect use of the equals sign, which assigns a boolean value instead of comparing it. The correct way to compare booleans is by using "==" or simply using the boolean variable without any comparison operator.
  • #1
ProPatto16
326
0
quick question, not sure why i can't 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:
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)
                    {
                        updateOldRecord();
                        System.out.println( "if executed");
                    }
                    else
                    {
                        newRecord();
                        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 doesn't 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 don't know why my if statement arguments arent working?

thanks
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Code:
There is a difference between assignment and comparison.

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

Code:
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:
if(b == true)
more concisely as
Code:
if(b)
 
  • #4
Thanks, knew it would be simple!
 
  • #5


I would suggest checking the logic of your if statement. In the code provided, the if statement is using the assignment operator (=) instead of the equality operator (==). This means that the statement will always evaluate to true, regardless of the value of the boolean variable contains. To fix this, you should use the equality operator in your if statement, as follows:

if (contains == true)

Additionally, you may want to consider using the contains() method directly on the ArrayList instead of iterating through it in a for loop. This could simplify your code and potentially avoid any logic errors.
 

Related to Why is my if statement executing when its false

Why is my if statement executing when its false?

There could be several reasons for this. One possibility is that there is a logical error in your code, causing the condition to evaluate to true even when it should be false. Another possibility is that there is a bug in the programming language or compiler you are using. It could also be due to unexpected input or data types causing the condition to be evaluated differently than expected.

How can I fix my if statement from executing when its false?

To fix this issue, you will need to carefully review your code and identify any potential logical errors. You can also try debugging your code to see if there are any unexpected inputs or data types causing the issue. If you suspect a bug in your programming language or compiler, you can try updating to the latest version or using a different language or compiler.

Is there a way to prevent my if statement from executing when its false?

Yes, there are a few ways to prevent this from happening. One option is to use a different conditional statement, such as a switch statement, to handle the logic. Another option is to add additional checks or validations to your if statement to ensure that it only executes when the condition is true. You can also try breaking down your if statement into smaller, more manageable parts to make it easier to debug and identify any issues.

Why is my if statement still executing when I have used the "else" keyword?

The "else" keyword will only execute if the condition in the if statement evaluates to false. If your if statement is still executing even with the "else" keyword, it could mean that there is a logical error in your code or that the condition is being evaluated differently than expected. You can try debugging your code or using alternative conditional statements to identify and fix the issue.

Can the order of my if and else statements affect the execution?

Yes, the order of your if and else statements can affect the execution. In most programming languages, the if statement will be evaluated first, and if the condition is false, the else statement will be executed. If you have multiple if statements with overlapping conditions, the order in which they are evaluated can change the outcome. It is essential to carefully review and organize your conditional statements to ensure the desired execution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
Back
Top