Abby:
Yo tendría que encontrar si la entrada del usuario para la contraseña contiene sólo 1 dígito y estoy teniendo problemas para lo que sería la sentencia if.
Yo actualmente sólo tengo:
import java.util.Scanner;
public static boolean onlyOneDigit (String password) {
if ( ........ ) {
System.out.print("Password requirement not met");
return true;
} else {
return false;
}
}
public static void main(String[] args) {
do {
System.out.print("Enter a password");
String password = input.next();
} while (onlyOneDigit(password));
{
System.out.print("Password updated");
}
}
WJS:
Prueba esto. de un solo dígito en la contraseña vs ningún dígito o más de uno de cada contraseña.
for (String pw : new String[]{"with1digit", "withtwo22digits","withNodigits",
"with2scatt2reddi3gits"}) {
boolean m = pw.matches("[^\\d]*\\d[^\\d]*");
System.out.println(m + " : " + pw);
}
Imprime el siguiente:
true : with1digit
false : withtwo22digits
false : withNodigits
false : with2scatt2reddi3gits
Ejemplo de uso.
if (onlyOneDigit(password)) {
// it's good
} else {
// warn user
}
public static boolean onlyOneDigit(String pw) {
return pw.matches("[^\\d]*\\d[^\\d]*");
}
Y ella es otra forma
public static boolean onlyOneDigit(String pw) {
// get rid of the first digit
String save =pw.replaceFirst("\\d","");
// replace the next digit with a #
save = save.replaceFirst("\\d", "#");
// if save contains # it had more than 1 digit
// or if save equals the original password
// then there were no digits.
return !(save.equals(pw) || save.contains("#"));
}