英文:
Char Array Values Not Holding User Input Values in JAVA
问题
我有一个字符数组被定义,并且其值等于来自控制台的用户输入。为了测试是否已经正确工作,我打印出字符数组中的每个值并检查是否正确。
char[] userPass = console.readPassword("🔒 请输入您的当前密码:");
char[] newPass, newPassConfirmation; // 用于存储新密码尝试的字符数组:
if (Arrays.equals(map.get(currentUser).toCharArray(), userPass)) {  // 如果密码正确,允许用户继续:
    boolean containsIllegal;
    boolean passwordsMatch = false; // 检查新密码和新密码确认是否匹配
    
    /*
    1. 获取第一个新密码尝试并将其存储在字符数组中。
    2. 然后获取密码确认尝试并将其存储在字符数组中。
    3. 比较密码并查看是否匹配。
    */
    do {
        do {
            newPass = console.readPassword("🔐 请输入您的新密码:");
            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("🔐 请再次输入您的新密码以确认:");
            containsIllegal = isValid(newPassConfirmation);
        } while (containsIllegal);
        if (Arrays.equals(newPass, newPassConfirmation)) {
            passwordsMatch = true;
        }
    } while (!passwordsMatch);
}
在 do-while 循环中,值被正确打印出来。如果用户输入了 "FISH",那么 for 循环将输出 FISH:
do {
    newPass = console.readPassword("🔐 请输入您的新密码:");
    for (int i = 0; i < newPass.length; i++) {
        //bw.write((char) newPass[i]);
        System.out.print(newPass[i]);
    }
    containsIllegal = isValid(newPass);
} while (containsIllegal);
问题是当我尝试在 do-while 循环之外打印出字符数组时,我得不到结果。如果用户输入了 FISH,输出将是空的,或者是一个空的字符数组:
for (int i = 0; i < newPass.length; i++) {
    //bw.write((char) newPass[i]);
    System.out.print(newPass[i]);
}
为什么我的 do-while 循环没有改变字符数组的元素?
这是 isValid 函数:
static boolean isValid(char[] userInput) {
    for (int i = 0; i < userInput.length; i++) {
        if (userInput[i] == ':') {
            System.out.println("\n🔮 您不能输入 ':',请重试。");
            Arrays.fill(userInput, (char) 0);
            return true;
        }
    }
    Arrays.fill(userInput, (char) 0);
    return false;
}
英文:
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;
}
答案1
得分: 1
newPass数组的引用被传递给了isValid函数,所以当你执行以下操作时:
Arrays.fill(userInput, (char) 0);
你正在用0填充newPass数组。由于无论密码是否有效,你都会执行这个操作,所以在调用isValid函数之后,数组总是会变成0。当你打印一个全为0的字符数组时,因为0是NULL字符,所以什么都不会被打印出来。
我认为isValid函数中的Arrays.fill调用没有任何意义。isValid函数不应该改变传入的数组。
另外,似乎isValid函数返回的是密码是否无效。你可能应该重新命名它...
static boolean isInvalidPassword(char[] userInput) {
        
    for(int i=0; i<userInput.length; i++) {
        if(userInput[i] == ':'){
            System.out.println("\n🚮 You cannot enter ':' please try again.");
            return true;
        }
    }
    return false;
}
英文:
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 <i>in</i>valid. You should probably rename that...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论