getText() vs getPassword()

后端 未结 4 1111
-上瘾入骨i
-上瘾入骨i 2020-12-01 17:57

I\'m currently designing a login system for a make-believe company, right now all I have is the Main login, which needs a lot of cleaning up. Below is my login handler.

4条回答
  •  甜味超标
    2020-12-01 18:09

    As all the other answers have said, JPasswordField returns a char[] when you call the getPassword() method. However, the way I have it set in my sample log on form is I have a method for validating the input. I have two arrays for storing usernames[] and passwords[] and then I have my username input, and my password input. The password input in my method changes the char[] to a string before continuing, you can do so like this:

     String PasswordTyped = new String(_pwd.getPassword());
    

    Then take that string and place that in your 'if' statement:

     if (_uid.equals("Nathan") && PasswordTyped.equals("password") {
         JOptionPane.showMessageDialog(null, "Congrats, you logged in as Nathan");
     }
    

    However, as I mentioned my validation method runs on the two arrays of usernames[] and passwords[], while accepting a string and a char[] as input. I will copy and paste my method so you can implicate it if you would like:

    public static void validate(String u, Char[] pass) {
        String password = new String(pass);
        boolean uGood = false;
        String[] usernames = new String[2];
        String[] passwords = new String[usernames.length];
    
        usernames[0] = "Don";
        passwords[0] = "password";
        usernames[1] = "Jared";
        passwords[1] = "password";
    
    
        for (int i = 0; i < usernames.length; i++) {
    
            if (usernames[i].equals(u) && passwords[i].equals(password)) {
                uGood = true;
            }
        }
        if (uGood) {
            System.out.println("Hooray, you did it!");
        }
        else {
            JOptionPane.showMessageDialog(null, "Incorrect Username\nand/or Password.");
        }
    }
    

    Finally, you would call this validation method by typing:

    validate(_uid.getText(), _pwd.getPassword());
    

提交回复
热议问题