motantan
motantan

Reputation: 32

Char Array Values Not Holding User Input Values in JAVA

I have a character array defined and its value is equal to that of the user input from the console. To test that this has worked correctly I print out each value in the character array and see if it is correct.

char[] userPass = console.readPassword("šŸ”’ Please enter your current password: ");

char[] newPass, newPassConfirmation; //Character arrays to store new password attempts:


if (Arrays.equals(map.get(currentUser).toCharArray(), userPass)) {  //If password is correct, allow user to prcoceed:
    
    boolean containsIllegal;
    boolean passwordsMatch = false; //See if new password and new confirmation passwords match 
    
    /*
    1. Get the first new password attempt and store it in a character array. 
    2. Then get the password confirmation attempt and store it in a character array.
    3. Compare the passwords and see if they match.
    */

    do{
        do {
            newPass = console.readPassword("šŸ”‘ Please enter your new password: ");
            for(int i = 0; i < newPass.length; i++) {
                //bw.write((char) newPass[i]);
                System.out.print(newPass[i]);
            };
            containsIllegal = isValid(newPass);
    } while (containsIllegal);

    for(int i = 0; i < newPass.length; i++) {
        //bw.write((char) newPass[i]);
        System.out.print(newPass[i]);
    };

    do {
        newPassConfirmation = console.readPassword("šŸ”‘ Please confirm your new password by entering it again: ");
        containsIllegal = isValid(newPassConfirmation);
    }while(containsIllegal);

    if(Arrays.equals(newPass, newPassConfirmation)) {
        passwordsMatch = true;
    }

}while(!passwordsMatch);

In the do-while loop the values print out correctly. If the user has entered "FISH" then the for loop print out FISH;

do {
    newPass = console.readPassword("šŸ”‘ Please enter your new password: ");
    for(int i = 0; i < newPass.length; i++) {
        //bw.write((char) newPass[i]);
        System.out.print(newPass[i]);
    };
    containsIllegal = isValid(newPass);
} while (containsIllegal);

The problem is when I try to print out the char array outside the do-while loop. I get no result. If the user entered FISH the output is nothing, or an empty character array?

for(int i = 0; i < newPass.length; i++) {
    //bw.write((char) newPass[i]);
    System.out.print(newPass[i]);
}

Why is my do while loop not changing the elements of the character array?

Here is the isValid

Sorry, here is the isValid function:

static boolean isValid(char[] userInput) {
        
    for(int i=0; i<userInput.length; i++) {
        if(userInput[i] == ':'){
            System.out.println("\nšŸ¤® You can not enter ':' please try again.");
            Arrays.fill(userInput, (char) 0);
            return true;
        }
    }       
    Arrays.fill(userInput, (char) 0);
    return false;
}

Upvotes: 0

Views: 44

Answers (1)

Sweeper
Sweeper

Reputation: 270780

A reference of the newPass array is passed to isValid, and so when you are doing:

Arrays.fill(userInput, (char) 0);

You are filling newPass with 0s. Since you are doing this whether or not the password is valid, the array will always be 0 after an isValid call. When you print an all-zero char array, nothing is printed, because 0 is the NULL character.

I don't think the Arrays.fill calls serve any purpose in isValid. isValid should not change the array passed in at all.

static boolean isValid(char[] userInput) {
        
    for(int i=0; i<userInput.length; i++) {
        if(userInput[i] == ':'){
            System.out.println("\nšŸ¤® You can not enter ':' please try again.");
            return true;
        }
    }
    return false;
}

Also, it seems like isValid is returning whether the password is invalid. You should probably rename that...

Upvotes: 1

Related Questions