0

I don't understand why the Exeption (below) exists. The Array isn't empty. Did I made a mistake with the assignation of the Characters in the Array? I try to encrypt some Words and use an two dimensionale Array to check the Letter and its values

    package encrypt;

    import java.util.ArrayList;
    import java.util.Arrays;

    public class Encrypt {

        public static char[][] getList = {
        //  {'L','P','N'}
            {'a','1','2'},
            {'b','2','2'},
            {'c','3','2'},
            {'d','1','3'},
            {'e','2','3'},
            {'f','3','3'},
            {'g','1','4'},
            {'h','2','4'},
            {'i','3','4'},
            {'j','1','5'},
            {'k','2','5'},
            {'l','3','5'},
            {'m','1','6'},
            {'n','2','6'},
            {'o','3','6'},
            {'p','1','7'},
            {'q','2','7'},
            {'r','3','7'},
            {'s','4','7'},
            {'t','1','8'},
            {'u','2','8'},
            {'v','3','8'},
            {'w','1','9'},
            {'x','2','9'},
            {'y','3','9'},
            {'z','4','9'}
        };

        private static ArrayList<Character> position = new ArrayList<Character>();
        private static ArrayList<Character> number = new ArrayList<Character>();
**strong text**
        public static void main(String[] args) {
            encrypt("Hello World!");
        }

        public static void encrypt(String input) {
            System.out.println(getPosition(input));
            System.out.println(getNumber(input));
        }

        private static String getPosition(String input) {
            for (int i = 0; i <= input.length(); i++) {
                for (int j = 0; j <= getList.length; j++) {

Exeption triggered if (input.charAt(i) == getList[j][1]) position.add(getList[j][1]); } } return stringBuilder(position); }

        private static String getNumber(String input) {
            for (int i = 0; i <= input.length(); i++) {
                for (int j = 0; j <= getList.length; j++) {

Exeption triggered
if (input.charAt(i) == getList[j][2]) number.add(getList[j][2]); } } return stringBuilder(number); }

        private static String stringBuilder (ArrayList arrayList) {
            return Arrays.toString(arrayList.toArray());        
        }
    }

Exeption:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 26 out of bounds
    for length 26
    at crypt.Crypt.getPosition(Crypt.java:53)
    at crypt.Crypt.crypt(Crypt.java:46)
    at crypt.Crypt.main(Crypt.java:42)

I don't know how to solve the problem

0 Answers0