作者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
1F:推 LPH66:我觉得问题好像是 tokens....检查一下 line 读到了啥吧 11/17 11:41
2F:推 darkk6:name[i]=tokens[0];name只有[0],while 在 i++ 之後会变成1 11/17 12:14
3F:→ ken915007:当然会ArrayIndexOutOfBoundsException... 11/17 14:32
4F:→ ken915007:int amount[][] = new int[10][12];所宣告出来的index只 11/17 14:34
5F:→ ken915007:是从0开始算,所以amount[0~9][0~11]… 11/17 14:36
6F:→ ken915007:抱歉~我看错了= = 11/17 14:37
7F:→ ken915007:我比较好奇的是跑第一笔资料就挂了?还是第二笔.... 11/17 14:43
8F:→ ken915007:要是第一笔就挂了~请看一楼的…第二笔才挂请看二楼的… 11/17 14:45