I am trying to use an if/else statement incorporating the java scanner for a username and password application -
this question has answer here:
- how compare strings in java? 23 answers
so asking user username, , if user types in "username" he/she have "correct, username!" displayed , if wrong "incorrect, not username!" displayed.
the problem having whenever type username, in example username being "username" have incorrect message displayed.
i know went wrong , if question has been asked million times if direct me 1 of posts has been solved great since can't seem find 1 that's problem.
import javax.swing.*; import java.util.scanner; public class userid { public static void main(string[] args) { scanner input = new scanner(system.in); string username; string password; string email; string scanner1; username = "username"; password = "password"; email = "emailhere"; system.out.println("------------------------------------------"); system.out.println("welcome id memory tester"); system.out.println("author: me"); system.out.println("------------------------------------------"); system.out.println("what username?"); scanner1 = (input.nextline()); if (scanner1 == username) { system.out.println("correct, username!"); } else { system.out.println("incorrect, not username!"); } }
}
==
operator compares references.
you need use .equals()
.
change to
if (scanner1.equals(username)) { system.out.println("correct, username!"); } else { system.out.println("incorrect, not username!"); }
Comments
Post a Comment