Java – exception in stackoverflow error of thread “main”
•
Java
I am writing a program to verify whether the password meets the corresponding requirements I've written all the code and I think it should work, but I received 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)
Then it repeats the last line of the error for a long time I've been looking around as if I couldn't figure out my problem I know something is constantly circulating. I don't want to, but the repair is not on me This 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 will be greatly appreciated!
Solution
public static boolean isValid(String str){
public static boolean isValid(String str){
// ...
if (PasswordVerifier.isValid(str) == true){
// ...
}
// ...
}
You call isvalid (string) internally, which results in infinite loop recursion
I'll guess and say that's 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;
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
