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;
}
isValidis 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.isValid