An extremely simple login system was one of the better ways I could think of to practice with Java conditionals. All I'm trying to do is compare two string variables, and it appears there's something wrong within that process. It always just goes to the final "else" statement, and I've done a lot of troubleshooting which has shown that the username and password are in fact the same by the time the conditionals come around.
Here's the code:
import java.util.Scanner;
public class HelloWorld {
// Pre-defined username and password requirements
String RequiredUsername = "test";
String RequiredPassword = "test";
// Initializes the textinput
Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// Creates a new "object" of the main program called loginpage
HelloWorld logingpage = new HelloWorld();
// Starts the "start" method
logingpage.start();
}
public void start() {
// // Main Screen in console
// System.out.println("+-----------------------------------------------+"
// + "| |"
// + "| Welcome to the Login Screen |"
// + "| |"
// + "+-----------------------------------------------+");
// Prompts the user for a username and gathers it
System.out.print("[*] Please enter Username\n>>> ");
String username = input.nextLine();
System.out.println("");
// Prompts the user for a password and gathers it
System.out.print("[*] Please enter Password\n>>> ");
String password = input.next();
System.out.println("");
// Conditionals to check username and password
if (username == RequiredUsername && password == RequiredPassword) {
System.out.println("[*] The secret message is: The eagle has landed!");
} else if (username == RequiredUsername && password != RequiredPassword) {
System.out.println("[*] ERROR, Password Not Valid");
} else if (username != RequiredUsername && password == RequiredPassword) {
System.out.println("[*] ERROR, Username Not Valid");
} else {
System.out.println("[*] ERROR, All Credentials Invalid");
}
// input.close();
}
}