It is hard to help you do this without giving you all the code to do this, since it is so short.
In any case, to begin with, since you need at least one letter and at least one number, you will need two flags, two booleans , of which initially will be false . You can iterate through char ininitialPassword using the foreach :
for (char c : initialPassword.toCharArray())
And then all you have to do is check at each iteration if c is possibly a letter or number, and set the appropriate flag if that is. When the loop ends, if both checkboxes are checked, then your password is valid. Here is what your code would look like:
boolean bHasLetter = false, bHasDigit = false; for (char c : initialPassword.toCharArray()) { if (Character.isLetter(c)) bHasLetter = true; else if (Character.isDigit(c)) bHasDigit = true; if (bHasLetter && bHasDigit) break; // no point continuing if both found } if (bHasLetter && bHasDigit) { /* valid */ }
source share