遇到 StringIndexOutOfBoundsException 怎么解决?java报错
在编写java的样例代码时出现这种问题。这种情况应该如何解决和避免那?
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;
}
错误:
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.lang.StringIndexOutOfBoundsException:字符串索引超出范围,越界了。