If statement using == gives unexpected result [duplicate]

You need to use String.equals(), not ==. == checks if two Object references refer to the same Object:

if("s".equals(word) || "t".equals(word) || ...

From section 15.21.3 Reference Equality Operators == and != of the Java Language Specification 3.0:

While == may be used to compare references of type String, such an equality
test determines whether or not the two operands refer to the same String
object. The result is false if the operands are distinct String objects, even if
they contain the same sequence of characters. The contents of two strings s and t
can be tested for equality by the method invocation s.equals(t).

Leave a Comment