java - 修复检查样式错误

标签 java eclipse checkstyle

我需要帮助解决 Eclipse 中不断出现的 checkstyle 错误。我已经尝试了 boolean 方法中的所有错误。它表明我的嵌套 if else 为 1,而它应该为零。这是我所有的 if 语句。我遇到的另一个错误是我的方法有 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;
}

}

最佳答案

样式检查器可能会提示很多。您可以通过将其设置更改为不那么挑剔来让它安静下来,或者您可以按照它的一些建议进行工作。在您的情况下,它不喜欢太多嵌套,也不喜欢多次返回。

返回之后的else可能是一个问题,所以你可以说

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

减少嵌套。如果多个 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;
}

这减少了return语句的数量,但嵌套级别仍然很高。也许将代码分解为更小的方法可能会有所帮助:

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

那么这里就没有嵌套了,但是代码的效率有点低,因为你对整个字符串运行每个测试。而且您还会有很多行代码。不过,我想 checkstyle 会更安静。

关于java - 修复检查样式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21298176/

相关文章:

java - 无法选择下拉选项

java - 在 Maven 存储库中使用已安装的 Java 版本

java - 运行时异常 - Datanucleus 增强器

javascript - 如何更改 Eclipse 以在 Javascript 编辑器中使用空格而不是制表符?

java - Android - vector 图像质量下降?

java - import 语句中的点变成红色 - github 比较

java - Eclipse有没有可能只打开一个JFrame?

plugins - Checkstyle Jenkins - 如何在 Jenkins 中配置 Checkstyle?

java - 如何找到失败的 checkstyle 规则的名称

java - 是否可以从 Checkstyle 中排除日志语句?