java - Why do these strings evaluate to false even when i enter them exactly right? -
this question has answer here:
- how compare strings in java? 23 answers
i writing simple program grips using scanner in java. program has stored value user_1 , asks user provide string variable username checked against user_1 using == operator, when username matches user_1 still evaluates false. i'm sure must missing simple, appreciated. added line printing both variable double check same!!
import java.util.scanner; public class helloworld { public static void main(string[] args) { string user_1 = ("lucasrhcp"); scanner input = new scanner(system.in); system.out.println("enter username: "); string username = input.nextline(); system.out.println(user_1 + " " + username); if (username == user_1){ system.out.println("true"); } else { system.out.println("false"); } } }
never compare objects (and string object) ==
, use equals()
method
Comments
Post a Comment