Here I have included my full code, The error is coming from the final part of my rule 3 method, I have tried other online resources but cannot understand how I am getting this error if anyone could guide me towards a solution I would be very grateful here is my code below:
import java.util.Scanner;
public class Assignment5E3{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.println("Please enter a new password: ");
String password = reader.next();
String firstRule = ruleOne(password);
String secondRule = ruleTwo(password);
String thirdRule = ruleThree(password);
}
public static String ruleOne(String password){
if( password.length() < 5 || password.length() > 12 ){
System.out.println("Password must be between 5 and 12 characters!");
}else{
System.out.println(password);
}
return password;
}
public static String ruleTwo(String password){
int j = 0;
int k = 0;
for(int i=0; i<password.length(); i++){
char ch = password.charAt(i);
if(Character.isLetterOrDigit(ch)){
j++;
}else if(!Character.isLetterOrDigit(ch)){
k++;
}
}
if(k > 0){
System.out.println("Password may only contain letters and numbers");
}
return password;
}
public static String ruleThree(String password){
char[] input = password.toCharArray();
char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
char[] numbers = "0123456789".toCharArray();
for(int i = 0; i<input.length-1; i++){
for(int j = 0; j<numbers.length-1; j++){
if((input[i] == numbers[j+1]) && (input[i+1] == numbers[j+2]) && (input[i+2] == numbers[j+3]) && (input[i+3] == numbers[j+4]) && (input[i+4] == numbers[j+5])){
System.out.println("Password cannot have 5 numbers in sequence!");
}
}
for(int k = 0; k<letters.length-1; k++){
if((input[i] == letters[k+1]) && (input[i+1] == letters[k+2]) && (input[i+2] == letters[k+3]) && (input[i+3] == letters[k+4]) && (input[i+4] == letters[k+5])){
System.out.println("Password cannot have 5 letters in sequence!");
}
}
}
return password;
}
}
I get the output I want from the code, but along with the output I am getting an outofbounds exception error. The point of this part of the code is to make sure that there is not 5 letters or numbers in sequence (e.g. 12345 and abcde consecutively within the password would result in invalid input for user). I am just trying to understand where the out of bound exception is coming from in my code. Thank you.