Domanda

I need help with the checkstyle errors i keep getting in eclipse. 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. This is for all my if statements. 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.

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;
}

}

È stato utile?

Soluzione

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

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:

// 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. 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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top