如何获取数据(属性)-以文本格式打开文件,逐行读取(OOP JAVA)



如何?在此处输入代码将读取的行拆分为多个部分;根据输入数据(零件(创建品牌;将该品牌添加到此列表中;

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

您需要使用Scanner读取文件,并在您的行中使用String.split来获取数据

最新更新