0

I am getting the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26 in my code when i try to run certain letters (e) and i dont know how to resolve it.

The array contains 26 characters(each letter of the alphabet). Can anybody see the problem in the code?

//Breaking up the letters from the input and placing them in an array
char[] plaintext = input.toCharArray();
//For loops that will match length of input against alphabet and move the letter 14 spaces
for(int i = 0;i<plaintext.length;i++) {
    for(int j = 0 ; j<25;j++) {
        if(j<=12 && plaintext[i]==alphabet[j]) {
            plaintext[i] = alphabet[j+14];
            break;
        }
        //Else if the input letter is near the end of the alphabet then reset back to the start of the alphabet
        else if(plaintext[i] == alphabet[j]) {
            plaintext[i] = alphabet [j-26];
        }
    }
}
1
  • 1
    Arrays start at 0. If it contains 26 entrys it will be in range 0-25 Commented May 6, 2013 at 12:32

4 Answers 4

4
if(j<=12 && plaintext[i]==alphabet[j]) {
     plaintext[i] = alphabet[j+14];
     break;
}

This code will access alphabet[26] if j == 12 and plaintext[i]==alphabet[j]. Your array has indexes 0-25. Java arrays have zero based indexes.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, the encryption is now working but now the decryption is giving the wrong output after the 14th letter. What do I need to change? ive updated the code for decrytion above.
@d99 This is a totally different question. Please accept any answer here, they are all more or less equally helpful. Then try to solve it yourself. After that, ask another question if needed.
Sorry, I tried it myself but cant really find what's wrong and I thought it would be relevant. I've accepted your answer, thanks.
No worries, but I recommend you to carefully think through your solution and possibly try a different approach. You will learn a lot more that way, and end up a better coder. :)
3

If it contains 26 characters (as you said) then the last index is 25 not 26. That causes the problem.

You have j<=12 so when j is 12 then you have index 26 (j+14) and that is out of array.

Comments

3

You have the edge case where j == 12 and you dereference alphabet[j+14] == alphabet[26].

Comments

3

When j is 12, you'll get 26. Since arrays in Java are zero-based arrays, your array indexes are from 0 to 25, so 26 is outOfBounds.

  if(j<=12 && plaintext[i]==alphabet[j]){
     //Check if j+14 doesn't exceed 25

Another thing, your for loops should be for(int j = 0 ; j<26;j++){ (don't worry, 25 is the last index).

BTW, the exception you're getting is very informative. Using a debugger will help a lot in cases like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.