java - Why do these strings evaluate to false even when i enter them exactly right? -


this question has answer here:

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

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -