Java - StringIndexOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19670369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:16:42  来源:igfitidea点击:

Java - StringIndexOutOfBoundsException

javastringclassoop

提问by G?khan Nas

First of all, here is the problematic part of my code; these are very basic classes

首先,这是我的代码有问题的部分;这些是非常基础的课程

public Passenger(String Name, String adress, String number, String password){
    count++;
    accId+=count;

    this.Name=Name;

    this.adress=adress;
    this.number=number;

    if(checkPw(password)==true){
        this.password=password;
    }

}

private boolean checkPw(String password){
    int length;
    length = password.length();

    if(length != 6){
        return false;
    }
    else if(password.charAt(0)==0){
        return false;
    }
    else {
        for (int i = 0; i < password.length();i++){
            if((password.charAt(i))==(password.charAt(i+1))){
                return false;
            }
        }
    }
    return true;        
}

testClass:

测试类:

public static void main(String[] args){
    Passenger gokhan=new Passenger("Gokhan","Istanbul","xxx","254651");

    System.out.println(gokhan.password);
}

So, I think the problem is in the Passenger class. Its my first time that class in class things (I meant the if(checkPw(password)==true) part). In the test class, it looks very clear and I never thought that this error will appear. How can I avoid this message?

所以,我认为问题出在Passenger 类上。这是我第一次在课堂上上课(我的意思是 if(checkPw(password)==true) 部分)。在测试类中,看起来很清晰,没想到会出现这个错误。如何避免此消息?

Full error:

完整错误:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6
    at java.lang.String.charAt(String.java:658)
    at project1.Passenger.checkPw(Passenger.java:45)
    at project1.Passenger.<init>(Passenger.java:27)
    at project1.testClass.main(testClass.java:11)

Java Result: 1

Java 结果:1

采纳答案by Alexis C.

The problem is here :

问题在这里:

for (int i = 0; i < password.length();i++){
    if((password.charAt(i))==(password.charAt(i+1))){
        return false;
    }
 }

When you're in the last iteration, you're trying to access the charin the stringat the position i+1which doesn't exists.

当你在最后一次迭代的时候,你要访问charstring该位置i+1不存在。

    text
       ^
       |
when i = 3 charAt(i) will return t and charAt(i+1) will throw the exception

回答by rgettman

This line appears to be the problem:

这一行似乎是问题所在:

if((password.charAt(i))==(password.charAt(i+1))){

When on the last iteration of the forloop, iis 5and i+1, or 6, goes off the end of the string, because indexes range from 0to length() - 1. The solution here is to stop the forloop iteration after the second-to-last character instead of the last character. Change

for循环的最后一次迭代中,iis 5andi+1或 or6离开字符串的末尾,因为索引范围从0length() - 1。这里的解决方案是在for倒数第二个字符而不是最后一个字符之后停止循环迭代。改变

for (int i = 0; i < password.length();i++){

to

for (int i = 0; i < password.length() - 1; i++){

So that the maximum value ihas in the forloop is 4, so i+1or 5isn't off the end of the string.

这样的最大值i已在for环路4,所以i+1还是5不脱字符串的结尾。