使用堆栈检查密码中相同数量的字母和数字

时间:2017-02-25 19:56:16

标签: java passwords stack

我必须编写一个程序来检查密码应该

  • 至少应为8个字符
  • 仅包含字母和数字(特殊字符)
  • 包含相同数量的字母和数字

程序应检查它是否有效并显示相应的消息。 此外,只应使用堆栈类作为数据结构。

这是我到目前为止所提出的:

public class dsa_1c {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc= new Scanner(System.in);

        String pass;

        System.out.println("Enter a password");
        pass= sc.nextLine();

        if(checkPass(pass)){
            System.out.println("Valid Password");
        }
        else{
            System.out.println("Invalid Password");
        }
    }


        public static boolean checkPass(String password){

            Stack<Character> stack= new Stack<Character>();


            int count1=0, count2=0;

            if(password.length()<8){
                return false;
            }
            else{
             for(int i=0; i<password.length();i++){
                 char c= password.charAt(i);

                  if (!Character.isLetterOrDigit(c)){
                    return false;}

                  if(Character.isLetterOrDigit(c)){

                    stack.push(c);

                  }

                    char top= stack.peek();

                  if(Character.isLetter(top)){
                      count1++;
                      stack.pop();                    

                  }

                  else if(Character.isDigit(top)){                  
                      count2++;
                      stack.pop();                    

                  }

                  if(count1==count2){
                 return true;
                  }

                  if(!stack.isEmpty()){
                 return false;
                  }

             }

            }
                return true;

            }

    }

运行时程序显示我输入的任何密码的“有效密码”,超过8个字符且没有特殊字符。 这部分显然是问题

if(count1==count2){
   return true;}

因为当我改变它时

if(count1!=count2)
    return false; }

它会返回任何有效密码的无效密码。

3 个答案:

答案 0 :(得分:1)

使用堆栈对我来说似乎有些过分 - 它足以迭代字符,计算数字和字母,并确保它们具有相同的数字:

private static boolean isValidPassword(String password) {
    int letterCount = 0;
    int digitCount = ;
    for (int i = 0; i < password.length(); ++i) {
        char c = password.charAt(i);
        if (Character.isLetter(c)) {
            ++letterCount;
        } else if (Character.isDigit(c)) {
            ++digitCount;
        } else {
            return false;
        }
    }
    return (letterCount + digitCount) >= 8 &&
           letterCount == digitCount;
}

答案 1 :(得分:1)

它的return true最终会导致问题。您可以只使用return count1 == count2;,而不是比较两个计数并返回值。以下是一个例子:

public static boolean checkPass(String password) {

    Stack<Character> stack = new Stack<Character>();
    int count1 = 0, count2 = 0;
    if (password.length() < 8) {
        return false;
    } else {
        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);

            if (!Character.isLetterOrDigit(c)) {
                return false;
            } else {
                stack.push(c);
            }
        }
        while(!stack.isEmpty()){
            char c = stack.pop();
            if(Character.isLetter(c)){
                count1++;
            }else{
                count2++;
            }
        }
        return count1 == count2;
    }
}

答案 2 :(得分:0)

假设这是一个操纵堆栈的练习,这是我的2美分:

public class dsa_1c {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a password");
    String pass = sc.nextLine();

    if (checkPass(pass)) {
        System.out.println("Valid Password");
    } else {
        System.out.println("Invalid Password");
    }
}

private static boolean checkPass(String pass) {
    boolean result = false;
    if (pass.length() == 8) {
        Stack stack = new Stack();
        for (char current : pass.toCharArray()) {
            if (stack.isEmpty()) {
                stack.push(current);
                continue;
            }
            char previousChar = stack.pop();
            if (sameType(current, previousChar)) {
                stack.push(previousChar);
                stack.push(current);
            }
        }
        if (stack.isEmpty()) {
            result = true;
        }
    }
    return result;
}

public static boolean sameType(char current, char previousChar) {
    boolean result = false;
    if (Character.isAlphabetic(current) && Character.isAlphabetic(previousChar)) {
        result = true;
    }
    else if (Character.isDigit(current) && Character.isDigit(previousChar)) {
        result = true;
    }
    return result;
}

static class Stack extends ArrayList {

    public static final char NULL_CHARACTER = 0x0;

    public void push(Character letter) {
        add(letter);
    }
    public Character pop() {
        Character result = NULL_CHARACTER;
        if (!isEmpty()) {
            result = (Character)remove(size()-1);
        }
        return result;
    }
}

}