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?
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