Can anyone fix this? What's wrong with my code? Accepts username and password as "admin"

System.out.print("Enter Username: ");
String user=myinput.readLine();
if (user=="admin") {

System.out.print("Enter Password: ");
String pass=myinput.readLine();
if (pass=="admin") {

statement(s)

} else if (pass!="admin") {
System.out.println("Invalid Password.");
}

} else if (user!="admin") {
System.out.println("Invalid Username.");
}

Recommended Answers

All 2 Replies

Compare strings with String.equals(), not the "==" operator.

user.equals("admin")

or if you want to avoid null pointer issues

"admin".equals(user)

It worked. Thanks a lot! That's a big help to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.