Reputation: 55
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!
Upvotes: 3
Views: 208
Reputation: 67502
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;
}
Upvotes: 7