Skip to content Skip to sidebar Skip to footer

If Statement Being Skipped During Execution

I am having a problem with this piece of code. The if statement on line 6 is being ignored during execution. I have stepped through the code at this point and the value of the vari

Solution 1:

You need to use .equals() to compare the actual value of two strings - otherwise you're checking if they're the same object.

if (files[position].equals("subjects.dat")) { 
    // do stuff
}

Solution 2:

Always check String equality using equals() method. == operator cheks if two refrenece variables point to the same object.


Solution 3:

Strings are objects in java and when you use "==", you compare the references (pointers), which usually are different (usually because for short strings some optimization is made). Long story short, use

string.equals(anotherString)

instead of

string == anotherString

Solution 4:

String - use .equals(). == will never give you true since the objects are different.

Instead of files[position] == "subjects.dat" use files[position].equals("subjects.dat")


Post a Comment for "If Statement Being Skipped During Execution"