【发布时间】:2020-12-13 12:30:59
【问题描述】:
例如:(我取了外部文件的第一行)
Giant, Colgate Toothpaste, 4.50
我想在将它们发送到对象和 ArrayList 之前/之后将它们分开并保存在这样的数组中。
mall[i] = "Giant";
product[i] = "Colgate Toothpaste";
price[i] = 4.50
p/s:我认为我应该这样做,因为我需要在未来改变价格。
这就是我的编码现在的样子。
public static void readFile(ArrayList<Product> productList) throws Exception {
try {
productList.clear(); //clear the list! or remove all elements from the list!
// Coding Here
}
catch(Exception e) { System.err.println(e.getMessage());}
}
下面是“product.in”文件(外部文件)的内容
Giant, Colgate Toothpaste, 4.50
Giant, Dashing Deodorant, 6.55
Giant, Adidas Deodorant, 7.55
Giant, Dettol Hand-sanitiser, 10.00
Giant, Sokubutso Shower Foam, 15.00
Tesco, Colgate Toothpaste, 4.55
Tesco, Dettol Hand-sanitiser, 7.00
Tesco, Sokubutso Shower Foam, 15.05
Tesco, Adidas Deodorant, 7.45
Tesco, Dashing Deodorant, 5.45
TF, Sokubutso Shower Foam, 15.05
TF, Dettol Hand-sanitiser, 9.50
TF, Adidas Deodorant, 8.55
TF, Dashing Deodorant, 7.55
TF, Colgate Toothpaste, 5.00
如果您认为我提供的信息较少,请回复此主题。我会提供更多。
edited: add product class
class Product {
private String store;
private String item;
private double price;
public Product(String store, String item, double price) {
this.setStore(store);
this.setItem(item);
this.setPrice(price);
}
【问题讨论】:
-
魔术关键字是 JSON。看一看:stackoverflow.com/questions/26605763/…
-
为什么不省略数组,直接把每一行变成
Product?当然,如果需要,您以后应该能够更改Product的价格? -
@Melvin 我不认为我可以使用 JSON,因为外部文件 (product.in) 已像上面的线程中那样预先格式化。
-
@KevinAnderson 我一直在将产品类更新到线程中。我不能因为它而省略数组。我需要向 Product 类发送 3 个数据
-
我认为我应该先将它们分开,然后再将它们发送到另一个班级
标签: java arrays arraylist filereader