我试图将输入文件中的数据存储到一系列对象中,然后存储在一个数组中。问题是它给了我一个错误消息,说我的数组超出了界限。Java - 对象数组越界问题
它从文件中获取数据没有问题,但我不知道为什么。
下面是我得到了什么:
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(new File("input.txt"));
Scanner keyboard = new Scanner(System.in);
int numItems = scan.nextInt();
scan.nextLine();
Books bookObject[] = new Books[numItems];
Periodicals periodicalObject[] = new Periodicals[numItems];
for(int i = 0; i < numItems; i++)
{
String tempString = scan.nextLine();
String[] tempArray = tempString.split(",");
if(tempArray[0].equals("B"))
{
char temp = 'B';
//THIS IS WHERE THE IDE SAYS THE ERROR IS
bookObject[i] = new Books(temp, tempArray[1],tempArray[2], tempArray[3], tempArray[4], tempArray[5]);
}
else if(tempArray[0].equals("P"))
{
char temp = 'P';
periodicalObject[i] = new Periodicals(temp, tempArray[1], tempArray[2], tempArray[3], tempArray[4], tempArray[5]);
}
}
}
这里的输入:
4
B,C124.S17,The Cat in the Hat,Dr. Seuss,Children’s Literature
P,QJ072.C23.37.4,Computational Linguistics,37,4,Computational Linguistics
P,QJ015.C42.55.2,Communications of the ACM,55,2,Computer Science
B,F380.M1,A Game of Thrones,George R. R. Martin,Fantasy Literature
询问技术问题101:1,显示错误。 2.显示你的输入。 :-) – 2012-02-22 05:39:01
StackOverflow!= Java调试器 – 2012-02-22 05:39:05
哦,很抱歉在编程板Lior上寻求编程帮助。下次我会考虑三次。 – mrowland 2012-02-22 05:50:32