Java if statements

Is there something wrong with this if statement, I'm trying to create a swing registration system ??? thank:)

 public void login()
    {
           String username = loginField.getText();
           char[] password = loginPass.getPassword();
           if (username.equals("test") && password.equals("test"))
           {
                    System.out.println("logged in");
           }
    }
+3
source share
5 answers

char [] does not match the string. Try creating a line from it:

new String(password).equals("test")
0
source

You might want

new String(password).equals("test")

instead of this. Comparing an array with a string does not make sense.

+6
source

. , . password char[]. :

1:

if (new String(password).equals("test"))

2: char char:

public boolean checkPassword(char[] pass, String correctPass)
{
    if (pass.length != correctPass.length()) return false;

    for (int i = 0; i < pass.length; i++)
    {
        if (pass[i] != correctPass.charAt(i)) return false;
    }
    return true;
}

if-statement

if (checkPassword(password, "test"))
+2

. "password" - - . String .

EDIT: , "" :

private static boolean isPasswordCorrect(char[] input) {
    boolean isCorrect = true;
    char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };

    if (input.length != correctPassword.length) {
        isCorrect = false;
    } else {
        isCorrect = Arrays.equals (input, correctPassword);
    }

    //Zero out the password.
    Arrays.fill(correctPassword,'0');

    return isCorrect;
}

java tutorials.

+1
password.equals("test")

false, test String password char [] - . .

char [] boolean java.utilArrays.equals(char [] a, char [] b):

java.util.Arrays.equals(password,"test".toCharArray())

String, char []. , , Arrays#equals.

+1

Source: https://habr.com/ru/post/1774724/


All Articles