它建立沒有問題,但是當我插上一語中的它給我的一般輸出框以下錯誤:問題與使用代碼陣列
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;
}
}
}
代碼太多了;這太離譜了。您需要編寫更少的代碼並更仔細地考慮數據結構。 – duffymo 2011-04-09 23:06:47
@duffymo - 有時我有一個可怕的夢想 - 網絡中的大部分代碼看起來像上面那樣。 – Ingo 2011-04-10 00:20:06