以文本格式打开文件以便逐行读取(oop java)

yvfmudvl  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(232)

此问题已在此处找到答案

是什么导致java.lang.arrayindexoutofboundsexception以及如何防止它((26个答案)
6天前关门。
怎样?在此处输入代码将读取的行拆分为多个部分;从输入数据(零件)创建品牌;将品牌添加到此列表中;

ArrayList<String> str1 = new ArrayList<>();
    loadFromFile(file, str1);    
    for (String string : str1) {
        String[] str = string.split(",");
        //EG line of file: B7-2018, BMW 730Li (2018), Harman Kardon, 3.749
        String id,name,sound;
        double price;

        id = str[0];
        name = str[1];
        price = Double.valueOf(str[3]);
         sound = str[2];
        Brands brand = new Brands(id, name, sound, price);
        this.add(brand);  /// this is arraylist of Brands (extends ArrayList Brands)
        //error code: ArrayIndexOutOfBoundsException 
        //I do not understand this
velaa5lx

velaa5lx1#

您需要使用 Scanner 和使用 String.split 在线获取数据

相关问题