[英]String Index Out of Bounds Exception Error for password program - NO arrays allowed
我為課堂編寫了一個程序,該程序具有以下要求:
提示用戶輸入新密碼。 長度必須為五個字符。 必須有一個大寫字母。 必須有一個小寫字母。 必須為一位。
如果不滿足這些條件中的任何一個,則輸出必須指出錯誤。 如果滿足所有條件,則輸出“密碼已成功更改”或類似的內容。 當我發現無法在程序中使用for循環時,便出現了問題,因為我們只能使用到目前為止所學到的知識來從事我們的創造力和解決問題的工作。 還明確指出我們不能使用數組。 我從頭開始執行程序,一切正常,直到我故意輸入四個字符以輸出“必須有五個字符”。 我得到了超出范圍異常錯誤的字符串索引,該錯誤表明我的charAt(4)是罪魁禍首。 據我了解,如果我輸入4個字符,程序會認為不存在第五個字符。 即使我在字符列表下方說明了長度條件,為什么仍顯示此錯誤? 偽代碼就足夠了,因為我更喜歡從指導中學習而不是復制代碼。 我所要查找的內容必須很小,因為除了鍵入的字符太少以外,其他所有東西都可以正常工作。 它顯示了相同的錯誤,只是更改了與丟失多少個字符有關的索引。
這是程序:
import java.util.Scanner;
public class ChangePassword1 {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
boolean hasUpper = false;
boolean hasLower = false;
boolean hasDigit = false;
boolean passLength = false;
System.out.print("Please enter your new password: ");
String newpassword = input.nextLine();
char c1 = newpassword.charAt(0);
char c2 = newpassword.charAt(1);
char c3 = newpassword.charAt(2);
char c4 = newpassword.charAt(3);
char c5 = newpassword.charAt(4);
if (newpassword.length() < 5 || newpassword.length() > 5) {
System.out.println("Your password must be five characters in length.");
}
if (!Character.isUpperCase(c1) && !Character.isUpperCase(c2) && !Character.isUpperCase(c3) && !Character.isUpperCase(c4) && !Character.isUpperCase(c5)) {
System.out.println("Your password must contain at least one uppercase letter.");
if (!Character.isLowerCase(c1) && !Character.isLowerCase(c2) && !Character.isLowerCase(c3) && !Character.isLowerCase(c4) && !Character.isLowerCase(c5))
System.out.println("Your password must contain at least one lowercase letter.");
if (!Character.isDigit(c1) && !Character.isDigit(c2) && !Character.isDigit(c3) && !Character.isDigit(c4) && !Character.isDigit(c5))
System.out.println("Your password must contain at least one digit.");
}
if (newpassword.length() == 5) {
passLength = true;
if (Character.isUpperCase(c1) || Character.isUpperCase(c2) || Character.isUpperCase(c3) || Character.isUpperCase(c4) || Character.isUpperCase(c5))
hasUpper = true;
if (Character.isLowerCase(c1) || Character.isLowerCase(c2) || Character.isLowerCase(c3) || Character.isLowerCase(c4) || Character.isLowerCase(c5))
hasLower = true;
if (Character.isDigit(c1) || Character.isDigit(c2) || Character.isDigit(c3) || Character.isDigit(c4) || Character.isDigit(c5))
hasDigit = true;
if (passLength == true && hasUpper == true && hasLower == true && hasDigit == true)
System.out.println("Password successfully changed.");
}
}
}
如果查看此塊,可能會看到錯誤:
char c1 = newpassword.charAt(0);
char c2 = newpassword.charAt(1);
char c3 = newpassword.charAt(2);
char c4 = newpassword.charAt(3);
char c5 = newpassword.charAt(4);
if (newpassword.length() < 5 || newpassword.length() > 5) {
System.out.println("Your password must be five characters in length.");
}
您嘗試使用charAt(4)
訪問第5個字符后 ,測試是否有5個字符的字符串。 嘗試在charAt
調用之前移動測試:這將防止您為不存在的String索引調用charAt
。
另外,您的測試可能更簡單。 如果必須是5個字符,為什么不僅僅測試一下newPassword.length()
是否等於( ==
)正確的長度?
在經歷了許多錯誤和陷阱之后,這是越界異常消失,返回的唯一途徑。 聲明。
System.out.print("Please enter your new password: ");
String newpassword = input.nextLine();
if (newpassword.length() != 5) {
System.out.println("Your password must be five characters in length.");
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.