0

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.

Michał Turczyn
  • 28,428
  • 14
  • 36
  • 58
saam
  • 13
  • 2

3 Answers3

1

Look at your loops. You loop until length-1 meaning the second last element. However, you want to look 5 elements ahead, and if you are at the fourth last element, there isn't a fifth element. Change it to length-5 and see if you get out of bounds.

Deggo
  • 82
  • 4
1

The main problem comes from your for loops in your ruleThree method.

For example when you use :

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

if password length is 8, i will go from 0 to 6. Then you test if one char is a digit with one condition which is :

(input[i+4] == numbers[j+5])

if i = 6 then i+4 = 10 > password.length giving you ArrayIndexOutOfBounds.

There are 2 possible approaches :

Change your loop final index :

for(int i = 0; i<input.length-5; i++)

Because you already check for password length > 5 chars.

Or you can use Java regex : https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html

For example :

Pattern pattern = Pattern.compile("\\d{5}");
//Get your password from your user input in String password;
Matcher matcher = pattern.matcher(password)
if(matcher.find()){
    System.out.println("Password cannot have 5 numbers in sequence!");
}
//You can do the same with letters

Java regex is really good for parsing and checking user input.

Edit : You can test your patterns in a java regex generator for example : https://regexr.com/

The regex I gave you will match for following input : pass12345 pa12345ss

and will not match pa1234ss pass1234

0

Note that your outer and inner loop that check numbers and letters are traversing to length-1. for example if you have password

acd123

it has length 6, your inner loop traverse 5 iterations. Inside your loop, when your iteration is 0, it tries to access 0(a), 1(c), 2(d), 3(1), 4(2) index to compare letter/number, that is valid. In second iteration, it tries to access 1(c), 2(d), 3(1), 4(2), 5(3) and in third iteration, it tries to access, 2(d), 3(1), 4(2), 5(3) ,6(no character on this index) that is invalid, because you have length of 6. same is the case with outer loops.

One simple way to solve this is to traverse all loop by length-5.

Hope this helps you.

Safwan Shaikh
  • 516
  • 2
  • 8