我无法让我的哨兵去工作java

时间:2014-10-28 18:58:57

标签: java sentinel

我正在编写一个程序来从用户那里获取密码,并确保它至少包含8个字符,1个大写和1个数字。如果你找不到密码,哨兵就会退出Q但是它不会工作。这就是我所拥有的......

import java.util.Scanner;

public class Chapter5ProblemSet {

    public static void main(String[] args) {   
        String password = getPassword();
        String validatePassword = validatePassword(password);
        displayPassword(validatePassword);

    }

    public static String getPassword(){
        String password = "";
        Scanner in = new Scanner (System.in);
        System.out.print("Please enter a password: ");
        password = in.next();
        return password;
    }



    //must be 8 characters, contain 1 uppercase, contain 1 digit
    public static String validatePassword(String password){
        String validatePassword = "";
        boolean oneUpperCase = false;
        boolean oneDigit = false;
        final String SENTINEL = "Q";
        Scanner in = new Scanner (System.in);

        while (!oneUpperCase || !oneDigit || password.length()< 8){
           validatePassword = "Please try again or enter Q to quit: ";
           System.out.print(validatePassword);
           password = in.next();

           oneUpperCase = !password.equals(password.toLowerCase());
           oneDigit = password.matches(".*\\d.*");
        } 
            validatePassword = "Password Ok";

        while(!password.equals(SENTINEL));
            System.out.print("Quit");
        return validatePassword;
    }

    public static void displayPassword(String validatePassword){

        System.out.println(validatePassword);
    }

}

2 个答案:

答案 0 :(得分:0)

您只需要第一个while循环:在那里检查SENTINEL值,例如

public static String validatePassword(String password) {
    String validatePassword = "";
    boolean oneUpperCase = false;
    boolean oneDigit = false;
    boolean exit = false;
    final String SENTINEL = "Q";
    Scanner in = new Scanner(System.in);

    while (!exit && (!oneUpperCase || !oneDigit || password.length() < 8)) {
        validatePassword = "Please try again or enter Q to quit: ";
        System.out.print(validatePassword);
        password = in.next();

        oneUpperCase = !password.equals(password.toLowerCase());
        oneDigit = password.matches(".*\\d.*");
        exit = password.equals(SENTINEL);
    }
    validatePassword = "Password Ok";

    System.out.print("Quit");
    return validatePassword;
}

答案 1 :(得分:0)

public static Boolean validPassword(String input){
  /* Simply leave out (?=.*[@#$%^&+=]) if you don't want at
   * least one special character
   */
  return input.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\S+$).{8,}$");

}

public String validatePassword(){

   Scanner in = new Scanner(System.in);
   String input = "";


   do {

     System.out.print("Enter password to validate or \"q\" to quit: ");
     input = in.next();

     if( input.lowercase().equals("q") ) {
       return "Cancelled";
     }

   } while( ! validPassword(input) );

   return "Password Ok";

}

(正则表达式从Tomalak's excellent answer on a similar question无耻地“借用”)