当我尝试运行某些字母(e)时,我在代码中收到错误 Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException
: 26 并且我不知道如何解决它。
该数组包含 26 个字符(字母表中的每个字母)。有人能看到代码中的问题吗?
//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];
}
}
}
最佳答案
if(j<=12 && plaintext[i]==alphabet[j]) {
plaintext[i] = alphabet[j+14];
break;
}
如果j == 12
并且plaintext[i]==alphabet[j]
,此代码将访问alphabet[26]
。您的数组具有索引0-25。 Java 数组具有从零开始的索引。
关于java - 线程中的异常 "main"java.lang.ArrayIndexOutOfBoundsException : 26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16398830/