开发者

Problem with code using arrays

It builds without a problem, but when I plug in the phrase it gives me the following error in the general output box:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    at java.lang.String.charAt(String.java:686)
    at encoder.main(encoder.java:77)

Process completed.

Can anyone tell me what the problem is and how to fix it?

Here is the code:

/**
 * @(#)encoder.java
 *
 *
 * @author Armando Herrera
 * @version Alpha Encoder 0.1 2011/4/9
 */
import javax.swing.*;
import java.lang.*;
import java.util.*;

public class encoder {

    /**
     * Creates a new instance of <code>encoder</code>.
     */
    public encoder() {
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String strencodersequense = JOptionPane.showInputDialog("Input a 9 digit number");///ask for sequenser///
        int digitlength = strencodersequense.length();
        if(digitlength != 9)
        {
            JOptionPane.showMessageDialog(null, "The number you entered is not desired lenght.");/////check if they put number of digits
            System.exit(0);
        }
        ///////////////////////////split the codersequense into char parts/////////////////////////////
        char firstdigitchar = strencodersequense.charAt(0);
        char seconddigitchar = strencodersequense.charAt(1);
        char thirddigitchar = strencodersequense.charAt(2);
        char fourthdigitchar = strencodersequense.charAt(3);
        char fifthdigitchar = strencodersequense.charAt(4);
        char sixthdigitchar = strencodersequense.charAt(5);
        char seventhdigitchar = strencodersequense.charAt(6);
        char eightdigitchar = strencodersequense.charAt(7);
        char ninthdigitchar = strencodersequense.charAt(8);
        //////////////////////////change split chars to strings///////////////////////////////////////
        String firstdigitstr = Character.toString(firstdigitchar);
        String seconddigitstr = Character.toString(seconddigitchar);
        String thirddigitstr = Character.toString(thirddigitchar);
        String fourthdigitstr = Character.toString(fourthdigitchar);
        String fifthdigitstr = Character.toString(fifthdigitchar);
        String sixthdigitstr = Character.toString(sixthdigitchar);
        String seventhdigitstr = Character.toString(seventhdigitchar);
        String eightdigitstr = Character.toString(eightdigitchar);
        String ninthdigitstr = Character.toString(ninthdigitchar);
        //////////////////////////change split strings into integers///////////////////////////////////////
        int firstdigitint = Integer.parseInt(firstdigitstr);
        int seconddigitint = Integer.parseInt(seconddigitstr);
        int thirddigitint = Integer.parseInt(thirddigitstr);
        int fourthdigitint = Integer.parseInt(fourthdigitstr);
        int fifthdigitint = Integer.parseInt(fifthdigitstr);
        int sixthdigitint = Integer.parseInt(sixthdigitstr);
        int seventhdigitint = Integer.parseInt(seventhdigitstr);
        int eightdigitint = Integer.parseInt(eightdigitstr);
        int ninthdigitint = Integer.parseInt(ninthdigitstr);
        /////////////////////////////////finished processing sequense////////开发者_开发技巧//////////////////////////////
        String toencode = JOptionPane.showInputDialog("Input phrase to encode, with no special characters and in lower caps" + 
                                                        " Note: it will not work with special characters.");//////asked phrase/////
        int toencodelenght = toencode.length();///get phrase lenght
        int passcode = firstdigitint * seconddigitint * thirddigitint * fourthdigitint * fifthdigitint *
                       sixthdigitint * seconddigitint * eightdigitint * ninthdigitint; ////make a combination of the digits////
        ////////////////////////////turn string into numbers////////////////////////////////////////////////////////////////////////////////////////

        int i = 0;

        char[] toencodecharacter = new char[toencodelenght];///start (making arrays)
        String[] toencodestringchar = new String[toencodelenght];
        int[] beforeencodenumber = new int[toencodelenght];
        int[] afterencodenumber = new int[toencodelenght];

        while(i <= toencodelenght)
            {
            toencodecharacter[i] = toencode.charAt(i); //////split characters//////
            toencodestringchar[i] = Character.toString(toencodecharacter[i]);
            if(toencodestringchar[i].equals("a")) //process the characters individually
                {
                    beforeencodenumber[i] = 1;
                }
            if(toencodestringchar[i].equals("b"))
                {
                    beforeencodenumber[i] = 2;
                }
            if(toencodestringchar[i].equals("c"))
                {
                    beforeencodenumber[i] = 3;
                }
            if(toencodestringchar[i].equals("d"))
                {
                    beforeencodenumber[i] = 4;
                }
            if(toencodestringchar[i].equals("e"))
                {
                    beforeencodenumber[i] = 5;
                }
            if(toencodestringchar[i].equals("f"))
                {
                    beforeencodenumber[i] = 6;
                }
            if(toencodestringchar[i].equals("g"))
                {
                    beforeencodenumber[i] = 7;
                }
            if(toencodestringchar[i].equals("h"))
                {
                    beforeencodenumber[i] = 8;
                }
            if(toencodestringchar[i].equals("i"))
                {
                    beforeencodenumber[i] = 9;
                }
            if(toencodestringchar[i].equals("j"))
                {
                    beforeencodenumber[i] = 10;
                }
            if(toencodestringchar[i].equals("k"))
                {
                    beforeencodenumber[i] = 11;
                }
            if(toencodestringchar[i].equals("l"))
                {
                    beforeencodenumber[i] = 12;
                }
            if(toencodestringchar[i].equals("m"))
                {
                    beforeencodenumber[i] = 13;
                }
            if(toencodestringchar[i].equals("n"))
                {
                    beforeencodenumber[i] = 14;
                }
            if(toencodestringchar[i].equals("o"))
                {
                    beforeencodenumber[i] = 15;
                }
            if(toencodestringchar[i].equals("p"))
                {
                    beforeencodenumber[i] = 16;
                }
            if(toencodestringchar[i].equals("q"))
                {
                    beforeencodenumber[i] = 17;
                }
            if(toencodestringchar[i].equals("r"))
                {
                    beforeencodenumber[i] = 18;
                }
            if(toencodestringchar[i].equals("s"))
                {
                    beforeencodenumber[i] = 19;
                }
            if(toencodestringchar[i].equals("t"))
                {
                    beforeencodenumber[i] = 20;
                }
            if(toencodestringchar[i].equals("u"))
                {
                    beforeencodenumber[i] = 21;
                }
            if(toencodestringchar[i].equals("v"))
                {
                    beforeencodenumber[i] = 22;
                }
            if(toencodestringchar[i].equals("w"))
                {
                    beforeencodenumber[i] = 23;
                }
            if(toencodestringchar[i].equals("x"))
                {
                    beforeencodenumber[i] = 24;
                }
            if(toencodestringchar[i].equals("y"))
                {
                    beforeencodenumber[i] = 25;
                }
            if(toencodestringchar[i].equals("z"))
                {
                    beforeencodenumber[i] = 26;
                }
            if(toencodestringchar[i].equals(" "))
                {
                    beforeencodenumber[i] = 27;
                }
            if(toencodestringchar[i].equals("1"))
                {
                    beforeencodenumber[i] = 28;
                }
            if(toencodestringchar[i].equals("2"))
                {
                    beforeencodenumber[i] = 29;
                }
            if(toencodestringchar[i].equals("3"))
                {
                    beforeencodenumber[i] = 30;
                }
            if(toencodestringchar[i].equals("4"))
                {
                    beforeencodenumber[i] = 31;
                }
            if(toencodestringchar[i].equals("5"))
                {
                    beforeencodenumber[i] = 32;
                }
            if(toencodestringchar[i].equals("6"))
                {
                    beforeencodenumber[i] = 33;
                }
            if(toencodestringchar[i].equals("7"))
                {
                    beforeencodenumber[i] = 34;
                }
            if(toencodestringchar[i].equals("8"))
                {
                    beforeencodenumber[i] = 35;
                }
            if(toencodestringchar[i].equals("9"))
                {
                    beforeencodenumber[i] = 36;
                }
            if(toencodestringchar[i].equals("0"))
                {
                    beforeencodenumber[i] = 37;
                }
            afterencodenumber[i] = beforeencodenumber[i] * passcode;
            if(i == toencodelenght)
                {
                    String numbers = "encoded with :" + strencodersequense + ": the encoded text is :" + Arrays.toString(afterencodenumber);
                    JOptionPane.showConfirmDialog(null, numbers);
                }
            i = i + 1;
            }

        }
    }


Try changing your while loop to be < not <=. When you are traversing an array of size length you always want to use < because the arrays are 0 indexed. If you have 9 elements, the last element is at index 8 and if you use <= in your loop, you will end up attempting to find the value at index 9 which is out of bounds.


In this case it is a String, not an Array, but the same logic applies. The index in cahrAt is allowed to range from 0 up to, but not including the lenght of the String.

Another comment: the over 30 if-statements are horrible. I am sure there is an easy algorithm that maps this letters to numbers. Sometimes, 5 minutes of thinking will save you 1/2h of typing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜