2011-04-09 74 views
0

它建立沒有問題,但是當我插上一語中的它給我的一般輸出框以下錯誤:問題與使用代碼陣列

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. 

誰能告訴我是什麼問題以及如何要解決這個問題?

下面是代碼:

/** 
* @(#)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; 
      } 

     } 
    } 
+4

代碼太多了;這太離譜了。您需要編寫更少的代碼並更仔細地考慮數據結構。 – duffymo 2011-04-09 23:06:47

+0

@duffymo - 有時我有一個可怕的夢想 - 網絡中的大部分代碼看起來像上面那樣。 – Ingo 2011-04-10 00:20:06

回答

6

試着改變你的while循環是<<=。當您遍歷一個大小爲length的數組時,您總是希望使用<,因爲這些數組是0索引的。如果您有9個元素,則最後一個元素位於索引8處,並且如果在循環中使用<=,則最終將嘗試在索引9處查找超出範圍的值。

+0

哦謝謝你,它的作品! – ggprogrammer467 2011-04-09 23:05:01

1

在這種情況下,它是一個字符串,而不是一個數組,但適用相同的邏輯。 cahrAt中的索引允許範圍從0到0,但不包括字符串長度的

另一個評論:超過30個if語句是可怕的。我確信有一個簡單的算法將這些字母映射到數字。有時候,5分鐘的想着會節省你1/2小時的打字。

+0

即使沒有算法來計算字母的數字,簡單的數組查找更容易編寫。 – 2011-04-10 00:36:11

+0

@Paulo,的確!可以肯定的是,一個數組只是一個(部分)映射函數,數組查找只是將該映射函數應用於一個值,所以我將其歸入「簡單算法」下。 (對不起,因爲無法在鍵盤上覆制名稱上的重音符號 - 希望它沒有太大區別)。 – Ingo 2011-04-10 00:42:41