Hello all. I am just practicing my Java and decided to make a super simple and insecure password program. This program has a hardcoded string that is the password, and gets user input as a string and compares the two. If they match its supposed to change a boolean (that determines if the user is currently logged in) from false to true; and if the user gets it wrong its supposed to output the string "bad password" and the value of the boolean as false.
The problem is no matter what is entered the wrong password string is displayed, even it the strings match. I have no idea why this is, everything compiles and runs, so no error messages. So perhaps im missing something obvious. Thanks in advanced for any help.
Code:
import java.util.Scanner; public class LogIn { public static void main(String[] args) { boolean userLoggedIn = false; boolean mainLoop = true; String passwd = new String("abc123"); //The password that must be entered Scanner inputPass; while(mainLoop) { inputPass = new Scanner(System.in); System.out.print("Enter password>"); String inputPassString = inputPass.next(); //Convert Scanner to String. if(inputPassString == passwd) { userLoggedIn = true; //User is now logged in. System.out.println("Logged in: " + userLoggedIn); mainLoop = false; } else { System.out.println("User input: " + inputPassString); //These two prove that strings match. System.out.println("User passwd: " + passwd); System.out.println("Bad pass. Logged in: " + userLoggedIn); //User should not be logged in. mainLoop = true; } } } }