简体   繁体   English

密码程序的字符串索引超出范围异常错误-不允许使用数组

[英]String Index Out of Bounds Exception Error for password program - NO arrays allowed

I wrote a program for class with the following requirements: 我为课堂编写了一个程序,该程序具有以下要求:

Prompt a use to enter a new password. 提示用户输入新密码。 Must be five characters in length. 长度必须为五个字符。 Must have one upper case letter. 必须有一个大写字母。 Must have one lower case letter. 必须有一个小写字母。 Must have one digit. 必须为一位。

If any of those conditions were not met, the output had to state the error. 如果不满足这些条件中的任何一个,则输出必须指出错误。 If all conditions were met, output "password successfully changed," or something similar. 如果满足所有条件,则输出“密码已成功更改”或类似的内容。 The problem arose when I discovered that I could not use the for loop in my program, since we are only supposed to use what we have learned up until now to work on our ingenuity and problem solving. 当我发现无法在程序中使用for循环时,便出现了问题,因为我们只能使用到目前为止所学到的知识来从事我们的创造力和解决问题的工作。 It was also specifically stated that we could not use arrays. 还明确指出我们不能使用数组。 I started my program over from scratch, and everything worked, up until I purposely entered four characters to output "must have five characters." 我从头开始执行程序,一切正常,直到我故意输入四个字符以输出“必须有五个字符”。 I got the string index out of bounds exception error that showed my charAt(4) being the culprit. 我得到了超出范围异常错误的字符串索引,该错误表明我的charAt(4)是罪魁祸首。 It is my understanding that if I typed 4 characters, the program thinks a fifth character doesn't exist. 据我了解,如果我输入4个字符,程序会认为不存在第五个字符。 Why is it showing this error even though I stated the length condition below the list of chars? 即使我在字符列表下方说明了长度条件,为什么仍显示此错误? Pseudocode will suffice since I prefer learn from guidance rather than copying code. 伪代码就足够了,因为我更喜欢从指导中学习而不是复制代码。 It has to be something small that I'm looking over, because everything works except typing too few characters. 我所要查找的内容必须很小,因为除了键入的字符太少以外,其他所有东西都可以正常工作。 It shows the same error, just changes the index in relation to how many characters are missing. 它显示了相同的错误,只是更改了与丢失多少个字符有关的索引。
Here is the program: 这是程序:

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.");
    }
  }
}

If you look at this block you might see the error: 如果查看此块,可能会看到错误:

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.");

}

You test to see that you have a string with 5 characters after you try and access the 5th character with charAt(4) . 您尝试使用charAt(4)访问第5个字符 ,测试是否有5个字符的字符串。 Try moving the test before the charAt calls: that will prevent you calling charAt for String indexes that don't exist. 尝试在charAt调用之前移动测试:这将防止您为不存在的String索引调用charAt

Also, your test could be simpler. 另外,您的测试可能更简单。 If it has to be 5 characters, why not just test to see if newPassword.length() is equal to ( == ) the correct length? 如果必须是5个字符,为什么不仅仅测试一下newPassword.length()是否等于( == )正确的长度?

After many errors and pitfalls, this was the only way the out of bounds exception would go away, the return; 在经历了许多错误和陷阱之后,这是越界异常消失,返回的唯一途径。 statement. 声明。

    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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM