0

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;
}
4
  • I'm guess isValid is doing something to the array. But for me to tell for sure, you need to provide a minimal reproducible example, the minimal amount of code that I can copy and paste into my IDE and reproduce the problem. Commented Oct 1, 2020 at 5:12
  • Sorry. I've updated the question to include isValid Commented Oct 1, 2020 at 5:15
  • As I thought, you are filling the array with 0s whether or not the password is valid. Why are you doing that at all? Why not just leave the array alone? Commented Oct 1, 2020 at 5:19
  • I see. I forgot that the array is passed by reference in Java, I thought I was merely making a copy of the array. Thank you! Commented Oct 1, 2020 at 5:30

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.