Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 密码程序的字符串索引越界异常错误-不允许数组_Java_String_Boolean_Indexoutofboundsexception_Chars - Fatal编程技术网

Java 密码程序的字符串索引越界异常错误-不允许数组

Java 密码程序的字符串索引越界异常错误-不允许数组,java,string,boolean,indexoutofboundsexception,chars,Java,String,Boolean,Indexoutofboundsexception,Chars,我为班级编写了一个程序,要求如下: 提示用户输入新密码。 长度必须为五个字符。 必须有一个大写字母。 必须有一个小写字母。 必须有一个数字 如果不满足这些条件中的任何一个,则输出必须说明错误。如果所有条件都满足,则输出密码成功更改,或类似情况。当我发现我不能在我的程序中使用for循环时,问题就出现了,因为我们应该只使用到目前为止所学的知识来进行创造性工作和解决问题。还有人明确指出,我们不能使用数组。 我从头开始我的程序,一切正常,直到我故意输入四个字符,输出必须有五个字符。我得到了字符串索引越界

我为班级编写了一个程序,要求如下:

提示用户输入新密码。 长度必须为五个字符。 必须有一个大写字母。 必须有一个小写字母。 必须有一个数字

如果不满足这些条件中的任何一个,则输出必须说明错误。如果所有条件都满足,则输出密码成功更改,或类似情况。当我发现我不能在我的程序中使用for循环时,问题就出现了,因为我们应该只使用到目前为止所学的知识来进行创造性工作和解决问题。还有人明确指出,我们不能使用数组。 我从头开始我的程序,一切正常,直到我故意输入四个字符,输出必须有五个字符。我得到了字符串索引越界异常错误,显示我的字符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.");

}
在尝试使用charAt4访问第5个字符后,测试是否有一个包含5个字符的字符串。尝试在charAt调用之前移动测试:这将阻止您为不存在的字符串索引调用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;
    } 

当我将测试放在charAt调用之前时,它会打印“必须是5个字符”消息,但它也会给我超出范围的异常索引:4。@csheridan。对如果你说我不应该给你完整的答案,你需要从程序中返回5个字符,只是一些指导。这听起来可能很愚蠢,但我不认为我可以从技术上使用这种方法,因为它还没有学会。这在后面的一章中。@csheridan。你是说回来?如果是这样的话,那么就用一个If equals==5,把程序的其余部分用它来包装。我的答案有用吗?如果是,请考虑使用复选标记。这向更广泛的社区表明,这是一个有用的答案,并给回答者和你自己带来了一些声誉。我注意到你加入后问了几个问题,但是-你应该养成接受问题最佳答案的习惯。谢谢。还在适应这个网站。我完全忽略了这一点。