作者PensiveMoon (淺色的那條)
看板java
標題[問題] ArrayIndexOutOfBoundsException: 1
時間Tue Nov 17 11:21:06 2009
這個問題困擾我好久,
我也不知道為什麼可以編譯,可是卻無法執行,
執行之後都會告訴我
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at ArrayTest.loadFile(ArrayTest.java:23)
at ArrayTest.main(ArrayTest.java:38)
我的檔案原始碼在下面。
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
//impor一些packge可以用
public class ArrayTest{
public void loadFile(String inputName){
//讀一個檔案,由下面的main呼叫
try{
FileInputStream fs = new FileInputStream(inputName);
InputStreamReader input = new InputStreamReader(fs);
BufferedReader bu = new BufferedReader(input);
//這裡是一些讀檔的操作
String line;
String name[]={"0"};
int amount[][] = new int[10][12];
int i=0;
int j;
//宣告應該要有的陣列之類的
while((line = bu.readLine()) != null){
String tokens[] = line.split(" ");
name[i] = tokens[0];
for(j=0;j<12;j++)
{
amount[i][j] = Integer.parseInt(tokens[j+1]);
//出現錯誤的行數
}
i++;
}
}
//傳進去的檔案,有十個人名,然後有他們相對應的十二個月的銷售金額數字
//不知道為什麼,我的amount那行怎麼樣都會出現錯誤,
//請問是否有高手可以幫忙看看.....
//謝謝!!
catch(FileNotFoundException x) {
x.printStackTrace();
}
catch(IOException x) {
x.printStackTrace();
}
}
public static void main(String args[]){
ArrayTest test = new ArrayTest();
test.loadFile("input.txt");
//出現錯誤的行數
}
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.118.237.182
推 LPH66:我覺得問題好像是 tokens....檢查一下 line 讀到了啥吧 11/17 11:41
推 darkk6:name[i]=tokens[0];name只有[0],while 在 i++ 之後會變成1 11/17 12:14
→ ken915007:當然會ArrayIndexOutOfBoundsException... 11/17 14:32
→ ken915007:int amount[][] = new int[10][12];所宣告出來的index只 11/17 14:34
→ ken915007:是從0開始算,所以amount[0~9][0~11]… 11/17 14:36
→ ken915007:抱歉~我看錯了= = 11/17 14:37
→ ken915007:我比較好奇的是跑第一筆資料就掛了?還是第二筆.... 11/17 14:43
→ ken915007:要是第一筆就掛了~請看一樓的…第二筆才掛請看二樓的… 11/17 14:45