3

I am writing a program that verifies if a password meets the appropriate requirements. I have all of my code written, and I feel it should work, but I get the following error:

Exception in thread "main" java.lang.StackOverflowError
at java.lang.String.length(String.java:623)
at PasswordVerifier.isValid(PasswordVerifier.java:5)
at PasswordVerifier.isValid(PasswordVerifier.java:6)

and then it repeats the last line of the error for quite some time. I've been looking around and cannot seem to figure out my issue. I know something is continually looping that I don't want to, but the fix eludes me. Here is my code

 public class PasswordVerifier{
  private static int MIN_PASSWORD_LENGTH = 6;

  public static boolean isValid(String str){
     if (str.length() >= MIN_PASSWORD_LENGTH){
        if (PasswordVerifier.isValid(str) == true){
           if (PasswordVerifier.hasUpperCase(str) == true){
              if (PasswordVerifier.hasLowerCase(str) == true){
                 if (PasswordVerifier.hasDigit(str) == true){
                    return true;
                 }
              }
           }
        }
     }
        return false;
  }

  private static boolean hasUpperCase(String str){
     for (char c : str.toCharArray()){
        if (Character.isUpperCase(c)){
           return true;
        }
     }
     return false;
  }

  private static boolean hasLowerCase(String str){
     for (char c : str.toCharArray()){
        if (Character.isLowerCase(c)){
           return true;
        }
     }
     return false;
  }

  private static boolean hasDigit(String str){
     for (char c : str.toCharArray()){
        if (Character.isDigit(c)){
           return true;
        }
     }
     return false;
  }
 }

Any help would be appreciated!

2
  • 3
    You can put all those nested ifs in one using &&. Better yet make it return the boolean expression. Commented Jan 25, 2013 at 22:20
  • You also can iterate over the string ONCE and not 3 times Commented Jan 25, 2013 at 22:38

1 Answer 1

7
public static boolean isValid(String str){
    // ...
        if (PasswordVerifier.isValid(str) == true){
        // ...
        }
    // ...
}

You're calling isValid(String) from within itself, which is causing the infinite loop recursion.

I'm going to take a wild guess and say this is what you want instead:

public static boolean isValid(String str){
    if (str.length() >= MIN_PASSWORD_LENGTH){
        // Removed call to .isValid(String)
        if (PasswordVerifier.hasUpperCase(str)){
            if (PasswordVerifier.hasLowerCase(str)){
                if (PasswordVerifier.hasDigit(str)){
                    return true;
                }
            }
        }
    }
    return false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

More of infinite recursion than infinite loop. +1 anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.