[問題] ArrayIndexOutOfBoundsException: 1
這個問題困擾我好久,
我也不知道為什麼可以編譯,可是卻無法執行,
執行之後都會告訴我
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
推
11/17 11:41, , 1F
11/17 11:41, 1F
推
11/17 12:14, , 2F
11/17 12:14, 2F
→
11/17 14:32, , 3F
11/17 14:32, 3F
→
11/17 14:34, , 4F
11/17 14:34, 4F
→
11/17 14:36, , 5F
11/17 14:36, 5F
→
11/17 14:37, , 6F
11/17 14:37, 6F
→
11/17 14:43, , 7F
11/17 14:43, 7F
→
11/17 14:45, , 8F
11/17 14:45, 8F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):