Exception in flow "major" StackoverFlow error
I am writing a program that checks if the password meets the appropriate requirements. I have all my code and I believe it should work, but I am getting 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 repeats the last line of error for some time. I looked around and couldn't figure out my problem. I know something is constantly looping which I don't want, but the fix is ββeluding 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!
+3
source to share
1 answer
public static boolean isValid(String str){
// ...
if (PasswordVerifier.isValid(str) == true){
// ...
}
// ...
}
You are calling isValid(String)
from within yourself, which causes an infinite loop recursion .
I'm going to take a wild guess and say this is exactly what you want:
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;
}
+7
source to share