我很擅长编程和上课.现在,我不是要求任何人为我编写代码,但我遇到了运行时错误.在赋值中,我们需要读取一个文件,使用第一行"15"来初始化数组的大小,然后用每行的信息填充数组.
编辑:我不想发布所有的代码,因为我觉得它看起来太长了但是由于模糊的暗示,这就是它.
文件:
15
produce,3554,broccoli,5.99,1
produce,3554,broccoli,5.99,1
produce,3555,carrots,2.23,0.25
produce,3555,carrots,2.23,0.25
produce,3555,carrots,2.23,0.25
cleaning,2345,windex,5.99,1 unit
cleaning,2345,windex,5.99,1 unit
cleaning,2345,windex,5.99,1 unit
cleaning,2345,windex,5.99,1 unit
cleaning,2346,toilet paper,12.99,4 rolls
cleaning,2346,toilet paper,12.99,4 rolls
cleaning,2335,windex,2.25,1 mini sprayer
cleaning,1342,wipes,3.99,10 units
cleaning,1342,wipes,3.99,10 units
produce,3546,lettuce,2.99,0.5
Run Code Online (Sandbox Code Playgroud)
我的错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at Inventory.readFile(Inventory.java:45)
at Inventory.<init>(Inventory.java:12)
at Supermarket.main(Supermarket.java:3)
Run Code Online (Sandbox Code Playgroud)
有问题的第45行的类(第45行被评论,向右滚动)"
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Inventory{
Product[] list;
String[] invData;
private int i = 0;
public int count;
public Inventory (String f){
readFile(f);
}
public int indexOfProduct(int code){
for(i=0; i<list.length; i++){
if (list[i] != null)
if (list[i].getCode() == code)
return i;
}
return -1;
}
public Product delete(int pos){
Product temp = new Product();
temp = list[pos];
list[pos] = null;
return temp;
}
public void readFile(String fileName){
try{
File invList = new File (fileName);
Scanner s = new Scanner(invList);
int itemCount = s.nextInt();
list = new Product[itemCount];
count = itemCount;
while (s.hasNext()){
String line = s.nextLine();
invData = line.split(",");
if (invData[0].equals("produce")){
list[i] = new Produce(invData[1], invData[2], invData[3], invData[4]); // This is Line 45, Where the error occurs
} else if(invData[0].equals("cleaning")){
list[i] = new Cleaning(invData[1], invData[2], invData[3], invData[4]);
}
i++;
}//end of while loop
} catch (FileNotFoundException Abra) {
String error = Abra.getMessage();
System.out.println(error);
}
} // end of method
public Product findCode(int c){
for(int i=0; i<list.length;i++)
if(list[1].getCode() == c)
return list[i];
return null;
}//end of method
}//end of class
Run Code Online (Sandbox Code Playgroud)
为什么我得到"ArrayIndexOutOfBoundsException"?我希望有人可以指出我的逻辑中的缺陷,所以我不再重复.
您的问题显然是使用i,因为这是该行唯一的变量索引,超出范围的索引是"15",这刚好超过15项数组的末尾.所以,几个问题,所有围绕使用i:
正如nhellwig所提到的,确保i在调用此函数之前实际上已将其初始化为0.
此外,您非常相信文件中项目编号的一致性和实际的项目数.您应该生成警告并停止尝试将项目存储在数组中i >= itemCount,或者使用像ArrayList这样的容器,它可以增长以容纳新项目而不是固定大小的数组.
编辑:另外,我应该指出,i无论你是否读过一个项目都会增加i,这意味着即使是空白行也会增加,导致列表或数组超出时出现空白.因为itemCount是项目的数字,你应该坚持这一点,只有i在你阅读实际项目时才会增加.
在同样的精神中,你应该invData.length == 5在调用split()之后验证,因为文件中错误的逗号等也可能最终出现OOB错误.当然,对于您的项目,可以对以"生产"或"清理"开头的行中的元素数量进行假设,但一般来说,对来自用户创建的文件的数据保持谨慎是很重要的.
我发现答案是我需要一个"s.nextLine();"
因为我使用了"s.nextInt();" 指针只是挂在我文件中"15"的末尾.然后,当While循环中的第一行"String line = s.nextLine();"时 执行指针从15的末尾移动到列表文件的第2行中的p in产生之前.
工作方法如下所示:
public void readFile(String fileName){
try{
File invList = new File (fileName);
Scanner s = new Scanner(invList);
int itemCount = s.nextInt();
s.nextLine(); // This is the new line that made it work
list = new Product[itemCount];
count = itemCount;
while (s.hasNext()){
String line = s.nextLine(); //moves file pointer over one
invData = line.split(",");
if (invData[0].equals("produce")){
list[i] = new Produce(invData[1], invData[2], invData[3], invData[4]);
} else if(invData[0].equals("cleaning")){
list[i] = new Cleaning(invData[1], invData[2], invData[3], invData[4]);
}
i++;
}//end of while loop
} catch (FileNotFoundException Abra) {
String error = Abra.getMessage();
System.out.println(error);
}
} // end of method
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
446 次 |
| 最近记录: |