数组索引超出范围异常。如果可以的话请帮忙

时间:2018-01-30 02:57:40

标签: java arrays encoding indexoutofboundsexception

非常接近完成这项任务,但无法看到这部分阻碍了我。如果有人能让我走上正轨,我将非常感激。以下是每次我尝试运行时eclipse给我的错误代码。

**线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:-2

at lab01.EncodeDecode.backMap(EncodeDecode.java:162)

at lab01.EncodeDecode.Decode(EncodeDecode.java:68)

at lab01.EncodeDecode。(EncodeDecode.java:26)

at lab01.EncodeDecodeTester.main(EncodeDecodeTester.java:14)**

 package lab01;
import java.util.*;
/**
 * 
 * @author David Bierbrauer, 
 *
 */
public class EncodeDecode 
{
    //method declaration
    static String[] originalList,encodedList,decodedList;
    static int total;

    public EncodeDecode(String[] oL) 
    {

        //instance variable declaration
        total = oL.length;
        originalList = new String[total];
        encodedList = new String[total];
        decodedList = new String[total];
        originalList = oL;


        encodedList= Encode(originalList);      
        decodedList = Decode(encodedList);
    }

    public static String[] Encode (String[] originalList)
    {
        //declare control variables
        String currentWord = "", codedWord = "";
        char currentChar = ' ';
        int i = 0, j = 0, stringLength = 0;


        for (i=0; i < total ; i++)
        {
            currentWord = originalList[i];
            stringLength = currentWord.length();

            for (j = 0; j < stringLength; j++)
            {
                currentChar = currentWord.charAt(j);
                codedWord = codedWord +forwardMap(currentChar);
            }

            encodedList[i] = codedWord;
            codedWord = "";
        }
        return encodedList;
    }

    public static String[] Decode (String[] encodedList) 
    {
        String currentWord = "", encodedWord = "";
        char currentChar = ' ';
        int i =0, j=0, stringLength = 0;

        for(i = 0; i < total; i++)
        {
            currentWord = encodedList[i];
            stringLength = currentWord.length();

            for(j = 0; j < stringLength; j++)
            {
                currentChar = currentWord.charAt(j);
                encodedWord = encodedWord + backMap(currentChar);
            }

            decodedList[i] = encodedWord;
            encodedWord = "";
        }
        return decodedList;
    }

    public static char forwardMap(char currentChar)
    {
        char newChar = ' ';
        int i = 0;

        String encodeMapUpper = "CDEFGHIJKLMNOPQRSTUVWXYZAB";
        String encodeMapLower = "cdefghijklmnopqrstuvwxyzab";
        String encodeMapNumber = "2345678901";

        char [] encodeArrayUpper = encodeMapUpper.toCharArray();
        char [] encodeArrayLower = encodeMapLower.toCharArray();
        char [] encodeArrayNumber = encodeMapNumber.toCharArray();

        if(encodeMapUpper.indexOf(currentChar) != -1)
        {
            for( i = 0; i < encodeArrayUpper.length; i++)
            {
                if(currentChar == encodeArrayUpper[i])
                {
                    newChar = encodeArrayUpper[(i+2) % 26];
                }
            }
        }

        else if(encodeMapLower.indexOf(currentChar) != -1)
        {
            for( i = 0; i < encodeArrayLower.length; i++)
            {
                if(currentChar == encodeArrayLower[i])
                {
                    newChar = encodeArrayLower[(i+2) % 26];
                }
            }
        }

        else if(encodeMapNumber.indexOf(currentChar) != -1)
        {
            for( i = 0; i < encodeArrayNumber.length; i++)
            {
                if(currentChar == encodeArrayNumber[i])
                {
                    newChar = encodeArrayNumber[(i+2) % 10];
                }

            }
        }
        else
        {
            //element is a special character
            newChar = currentChar;
        }

        return newChar;
    }

    public static char backMap(char currentChar)
    {
        char newChar = ' ';
        int i = 0;

        String decodeMapUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String decodeMapLower = "abcdefghijklmnopqrstuvwxyz";
        String decodeMapNumber = "0123456789";

        char[] decodeArrayUpper = decodeMapUpper.toCharArray();
        char[] decodeArrayLower = decodeMapLower.toCharArray();
        char[] decodeArrayNumber = decodeMapNumber.toCharArray();

        if (decodeMapUpper.indexOf(currentChar) != -1)
        {
            for (i=0; i < decodeArrayUpper.length; i++)
            {
                if (currentChar == decodeArrayUpper[i])
                {
                    newChar = decodeArrayUpper[(i - 2) % 26];
                }
            }
        }

        else if(decodeMapLower.indexOf(currentChar) != -1)
        {
            for (i=0; i < decodeArrayLower.length; i++)
            {
                if (currentChar == decodeArrayLower[i])
                {
                    newChar = decodeArrayLower[(i - 2) % 26];
                }
            }
        }

        else if(decodeMapNumber.indexOf(currentChar) != -1)
        {
            for (i=0; i < decodeArrayNumber.length; i++)
            {
                if (currentChar == decodeArrayNumber[i])
                {
                    newChar = decodeArrayNumber[(i - 2) % 10];
                }
            }
        }   

        else
        {
            newChar = currentChar;
        }
        return newChar;
    }
    //get methods
    public String[] getEncodedList() { return encodedList;}
        public String[] getDecodedList() { return decodedList;}

}

以下是测试人员类别。

package lab01;

public class EncodeDecodeTester 
{
    public static void main(String[] args)
    {
        EncodeDecode testEncoder;
        int x = 0;
        String[] output = new String[5];
        String[] oL = new String[] {"catdog","24","keys","Duck","PIZZA!"};


        //create encoder
        testEncoder = new EncodeDecode(oL);

        System.out.println("Encoded list:");
        for( x = 0; x < output.length; x++)
        {
            output = testEncoder.getEncodedList();
            System.out.println(output[x]);
        }

        System.out.println();
        System.out.println("Decoded List:");
        for(x = 0; x < output.length; x++)
        {
            output = testEncoder.getDecodedList();
            System.out.println(output[x] + " ");
        }
        System.out.println();

        System.out.println("End");
    }
}

请帮助我完全迷失在这里我做错了的话。

1 个答案:

答案 0 :(得分:1)

Java %运算符并不总是生成介于0和第二个操作数之间的数字。用(i - 2) % 26替换(i + 24) % 26(可以产生-2),在其他地方也是如此。