下面的代碼給出了問題,我只需要將字符串中的字母轉換爲字符,並且當我運行我的測試時,當代碼變爲char c = t.charAt(0);
時,我不斷收到錯誤。確切的錯誤消息是:將字符串轉換爲char時的StringIndexOutOfBoundsException。可能的原因?
java.lang.StringIndexOutOfBoundsException:字符串索引超出範圍:0
我不能讓它只是把字母串到一個char。任何提示將非常感謝。
String[] zombies;
int num = 0;
Vector<Zombie> practice = new Vector<Zombie>();
String zombieString = "SZI1";
zombies = zombieString.split("");
for (String t : zombies) {
if (isNumeric(t)) {
int multiplier = Integer.parseInt(t);
String extraZombie = zombies[num - 1];
char x = extraZombie.charAt(0);
for (int i = 0; i <= multiplier; i++) {
Zombie zombie = Zombie.makeZombie(x);
practice.add(zombie);
}
} else {
char c = t.charAt(0);
//Zombie zombie = Zombie.makeZombie(c);
//practice.add(zombie);
num++;
}
}
sooo,有什麼錯誤? – 2013-04-21 01:11:32
你用來獲得殭屍的分割看起來不太有用。讓我看看 - 那個錯誤表明它等於空字符串。你怎麼看? – duffymo 2013-04-21 01:12:30
「t」可能是空字符串。 @duffymo我認爲他正試圖將這些字符串分成單個字符或許? – KyelJmD 2013-04-21 01:13:44