import javax.swing.JOptionPane;

public class Login{
    public static void main(String[] args){
        String username = JOptionPane.showInputDialog("What is the desired username?");
        String password = JOptionPane.showInputDialog("What is the desired password?");
        boolean unlock=false;
        while(unlock==false){
        String usernameinput = JOptionPane.showInputDialog("What is the username?");
        if(username==usernameinput){
            String passwordinput = JOptionPane.showInputDialog("What is the password?");
            if(passwordinput==password){
            System.out.println("access granted");
            unlock=true;
            }else{
            System.out.println("denied");
            }
        }else{
            System.out.println("denied");
        }
        }
    }
}

This code should allow me to input a username and password when prompted then show that access is granted when they are equal but the program seems to not allow it. Am I doing something wrong here?

Recommended Answers

All 2 Replies

string1 == string2 tests for those being exactly the same object (both have the saem memory address), which they're not in your case.
The equals method for Strings tests if two Strings contain eactly the same sequence of letters - which is what you need. You'll find the documentation for equals in the API doc for the String class (as always)

Thanks I didn't realise that .equals() and == were not interchangeable.

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.