简体   繁体   English

修复checkstyle错误

[英]Fixing checkstyle error

I need help with the checkstyle errors i keep getting in eclipse. 我需要帮助我在日食中遇到的checkstyle错误。 I have tried everything the error is in my boolean method. 我已经尝试了我的布尔方法中的错误。 It is stating that my nested if else is at 1 when it is supposed to be at zero. 它声明我的嵌套if else在它应该为零时为1。 This is for all my if statements. 这适用于我所有的if语句。 Another error i had was that my method has 3 returns and checkstyle says the max is 2. I just want to rid myself of these errors can someone please help me. 我遇到的另一个错误是我的方法有3个返回,checkstyle说最大值是2.我只是想摆脱这些错误,有人可以帮助我。

public class Password {
private String potentialpassword;
private static final String SPECIAL_CHARACTERS = "!@#$%^&*()~`-=_+[]{}|:\";',./<>?";

/**
 * initializes the potential password and takes it as a string.
 * 
 * @param potentialpassword
 *            takes in the potential password
 * 
 */
public Password(String potentialpassword) {
    super();
    this.potentialpassword = potentialpassword;

}

/**
 * The purpose of this method is to validate whether the password meets the
 * criteria that was given
 * 
 * @param potentialpassword
 *            allows a string potential password to be accepted.
 * @return true or false if the method fits a certain guideline.
 * @precondition password has to be greater than 6 characters long. password
 *               also cannot contain any whitespace and the digits cannot be
 *               less than one.
 */
public static boolean isValid(String potentialpassword) {
    if (potentialpassword.length() < 6) {
        return false;
    } else {

        char x;
        int count = 0;
        for (int i = 0; i < potentialpassword.length(); i++) {
            x = potentialpassword.charAt(i);
            if (SPECIAL_CHARACTERS.indexOf(String.valueOf(x)) >= 1) {
                return true;
            }
            if (Character.isWhitespace(x)) {
                return false;
            }
            if (Character.isDigit(x)) {
                count++;
            } else if (count < 1) {
                return false;
            }

        }
        return true;
    }
}

/**
 * Print the potential string characters on a separate line.
 * 
 * @return the potential password characters on each line.
 * 
 * 
 */
public String toString() {
    String potentialpassword = "w0lf@UWG";
    for (int i = 0; i < potentialpassword.length(); i++) {
        System.out.println(potentialpassword.charAt(i));
    }
    return potentialpassword;
}

} }

Style checkers can complain a lot. 风格检查员可以抱怨很多。 You can quiet it down by changing its settings to not be so picky, or you can work on some of its suggestions. 您可以通过将其设置更改为不那么挑剔来安静下来,或者您可以处理其中的一些建议。 In your case it does not like too much nesting and it does not like multiple returns. 在你的情况下,它不喜欢太多的嵌套,它不喜欢多次返回。

The else after a return might be an issue, so you could say return后的else可能是一个问题,所以你可以说

if (potentialpassword.length() < 6) {
    return false;
}
char x;
int count = 0;
for (int i = 0; i < potentialpassword.length(); i++) {
    .
    .
    .

to reduce nesting. 减少嵌套。 If the multiple return statements is more of a problem you can try: 如果多个return语句更有问题,您可以尝试:

// NOTE I am just copying over your code and not worrying about the algorithm's correctness

boolean valid = true;
if (potentialpassword.length() < 6) {
    valid = false;
} else {
    char x;
    int count = 0;
    for (int i = 0; i < potentialpassword.length(); i++) {
        x = potentialpassword.charAt(i);
        if (SPECIAL_CHARACTERS.indexOf(String.valueOf(x)) >= 1) {
            valid = true;
            break;
        }
        if (Character.isWhitespace(x)) {
            valid = false;
            break;
        }
        if (Character.isDigit(x)) {
            count++;
        } else if (count < 1) {
            valid = false;
            break;
        }
    }
    return valid;
}

which reduces the number of return statements, but the nesting level stays high. 这减少了return语句的数量,但嵌套级别仍然很高。 Perhaps breaking your code into smaller methods may help: 将代码分解为更小的方法可能会有所帮助:

public static boolean isValid(String potentialpassword) {
    return potentialpassword.length >= 6 &&
           containsAtLeastOneSpecialCharacter(potentialpassword) &&
           !containsWhitespace(potentialpassword) &&
           startsWithADigit(potentialpassword);
}

Then you have no nesting here, but the code is a little less efficient because you run each test on the whole string. 那么你在这里没有嵌套,但代码效率稍差,因为你在整个字符串上运行每个测试。 And you will have quite a few more lines of code. 而且你会有更多的代码行。 I would imagine checkstyle would be quieter, though. 不过我觉得checkstyle会比较安静。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM