2014-01-23 108 views
0

我需要帮助我检查日志中的checkstyle错误。我已经尝试了所有错误是在我的布尔方法。这是说,我的嵌套if else在1时它应该是零。这是我所有的if语句。我的另一个错误是,我的方法有3个返回和checkstyle说,最大值是2.我只是想摆脱自己的这些错误可以有人请帮助我。修复checkstyle错误

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

/** 
* 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 = "[email protected]"; 
    for (int i = 0; i < potentialpassword.length(); i++) { 
     System.out.println(potentialpassword.charAt(i)); 
    } 
    return potentialpassword; 
} 

}

+0

你确定你的算法是正确的吗?在我看来,您拒绝以纯文字开头的密码。 –

回答

2

风格检查可以抱怨很多。你可以通过改变它的设置来减少噪音,或者你可以尝试一些建议。在你的情况下,它不喜欢太多的嵌套,它不喜欢多个返回。

else一个return后可能是一个问题,所以你可以说

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会更安静。