2014-01-23 106 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會更安靜。