0
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?

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by jarograv
0

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)

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.