public static void read() throws NumberFormatException, IOException //读取文件中的信息 { BufferedReader bufferedreader = new BufferedReader(new FileReader(FILE_NAME)); //创建一个字符流缓冲对象 String line; while ((line = bufferedreader.readLine()) != null) { String[] parts = line.split(","); //将line通过“ ,”分割字符串数组 String Gname = parts[0]; int Gage = Integer.parseInt(parts[1]); int Gphonenumber=Integer.parseInt(parts[2]); String Gsex = parts[3]; String Gadr = parts[4]; String Gsize = parts[5]; //按照顺序,将数组中的读取出来 Guke guke = new Guke(Gname, Gage, Gphonenumber, Gsex, Gadr, Gsize); //将读取出来的通过Guke的构造方法创建类 customerList.add(guke); //将Guke类存进Arraylist中 customerNames.add(Gname); // 将名字存进另一个Arraylist中 } } 使用这个从文件中读取信息,为什么会抛出异常Index 4 out of bounds for length 0
时间: 2024-02-07 12:03:47 浏览: 23
该异常 "Index 4 out of bounds for length 0" 表示你尝试访问一个长度为零的数组中的第五个元素,即 `parts[4]`,这通常是因为你尝试解析的某一行数据没有足够的逗号分隔符来分割成六个部分,导致数组 `parts` 的长度不足 6。
你需要检查文件中的每一行数据,确保每个数据都能按逗号分隔成六部分。如果有任何数据不符合你的格式,你需要进行适当的处理,例如跳过该行或使用默认值来代替缺失的数据。
相关问题
public class Inventory { static Vector<Goods> goods = new Vector<>(); static Vector<EventO> eventO = new Vector<>(); static Vector<EventR> eventR = new Vector<>(); static Vector<EventD> eventD = new Vector<>(); static Vector<EventA> eventA = new Vector<>(); static Vector<Event> v_shipping = new Vector<>(); static Vector<Event> v_errors = new Vector<>(); public static void main(String[] args) { try { get_Inventory(); //读取库存记录 get_Transactions(); } catch (IOException e) { e.printStackTrace(); //读取事件 } EventA_Handle(); //处理A事件 EventR_Handle(); //处理R事件 EventO_Handle(); //处理O事件 EventD_Handle(); //处理D事件 File_Handle(); //生成shipping、Errors、NewInventory文件 } /** * @description 读取库存记录,存入vector中 */ static void get_Inventory() throws IOException { BufferedReader in = new BufferedReader(new FileReader("src/Inventory.txt")); String line; while((line = in.readLine())!=null) { //按行读取文件 String[] str2 = line.split("\\s+"); //分解字符串 Goods goods_temp = new Goods(str2[0], Integer.parseInt(str2[1]), str2[2], str2[3]); goods.addElement(goods_temp); } in.close(); } /** * @description 读取库存记录,存入vector中 */ static void get_Transactions() throws IOException { //读取事件 BufferedReader in = new BufferedReader(new FileReader("src/Transactions.txt")); String line; while((line = in.readLine())!=null) { //按行读取文件 String[] str2 = line.split("\\s+"); //分解字符串 if (str2[0].equals("O")) { //读取O事件 EventO event_temp = new EventO(str2[1], Integer.parseInt(str2[2]), str2[3]); eventO.addElement(event_temp); } else if (str2[0].equals("R")) { //读取R事件 EventR event_temp = new EventR(str2[1], Integer.parseInt(str2[2])); eventR.addElement(event_temp); } else if (str2[0].equals("D")) { //读取D事件 EventD event_temp = new EventD(str2[1]); eventD.addElement(event_temp); } else if (str2[0].equals("A")) { //读取A事件 EventA event_temp = new EventA(str2[1], str2[2], str2[3]); eventA.addElement(event_temp); } } in.close(); }请为以上代码添加注释
/**
* Inventory类用于处理库存和事件记录相关的操作
*/
public class Inventory {
static Vector<Goods> goods = new Vector<>(); //存放商品记录的向量
static Vector<EventO> eventO = new Vector<>(); //存放O事件记录的向量
static Vector<EventR> eventR = new Vector<>(); //存放R事件记录的向量
static Vector<EventD> eventD = new Vector<>(); //存放D事件记录的向量
static Vector<EventA> eventA = new Vector<>(); //存放A事件记录的向量
static Vector<Event> v_shipping = new Vector<>(); //存放shipping事件记录的向量
static Vector<Event> v_errors = new Vector<>(); //存放Errors事件记录的向量
/**
* 库存及事件记录处理的程序入口
* @param args
*/
public static void main(String[] args) {
try {
get_Inventory(); //读取库存记录
get_Transactions(); //读取事件记录
} catch (IOException e) {
e.printStackTrace();
}
EventA_Handle(); //处理A事件
EventR_Handle(); //处理R事件
EventO_Handle(); //处理O事件
EventD_Handle(); //处理D事件
File_Handle(); //生成shipping、Errors、NewInventory文件
}
/**
* 从文件中读取库存记录,存入商品记录向量中
* @throws IOException
*/
static void get_Inventory() throws IOException {
BufferedReader in = new BufferedReader(new FileReader("src/Inventory.txt"));
String line;
while((line = in.readLine())!=null) { //按行读取文件
String[] str2 = line.split("\\s "); //分解字符串
Goods goods_temp = new Goods(str2[0], Integer.parseInt(str2[1]), str2[2], str2[3]);
goods.addElement(goods_temp);
}
in.close();
}
/**
* 从文件中读取事件记录,分别存入对应的事件记录向量中
* @throws IOException
*/
static void get_Transactions() throws IOException {
BufferedReader in = new BufferedReader(new FileReader("src/Transactions.txt"));
String line;
while((line = in.readLine())!=null) { //按行读取文件
String[] str2 = line.split("\\s "); //分解字符串
if (str2[0].equals("O")) { //读取O事件
EventO event_temp = new EventO(str2[1], Integer.parseInt(str2[2]), str2[3]);
eventO.addElement(event_temp);
} else if (str2[0].equals("R")) { //读取R事件
EventR event_temp = new EventR(str2[1], Integer.parseInt(str2[2]));
eventR.addElement(event_temp);
} else if (str2[0].equals("D")) { //读取D事件
EventD event_temp = new EventD(str2[1]);
eventD.addElement(event_temp);
} else if (str2[0].equals("A")) { //读取A事件
EventA event_temp = new EventA(str2[1], str2[2], str2[3]);
eventA.addElement(event_temp);
}
}
in.close();
}
}
public static void main(String args[]) throws IOException { Scanner sc=new Scanner(System.in);
这是 Java 中一个简单的主函数,它使用 Scanner 类从命令行读取输入。具体来说,它创建了一个名为 sc 的 Scanner 对象,并将其与标准输入流 System.in 关联。接下来,它可以使用 sc 对象调用 Scanner 类中的方法来读取用户在命令行中输入的数据。在这个示例中,它使用了 throws IOException 来处理可能的输入输出异常。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)