'How do I convert Strings of an ArrayClass into Objects?
I'm trying to construct a for-each loop that converts individual Strings of an ArrayList to objects of the Product class, so I may add them to an Array.
public Order(String fileName, String order_id) {
orderID = order_id + " JM";
file = new FileReader(fileName);
file.getLines();
ArrayList<String> lineList = file.getLines();
product_list = new ArrayList<Product>();
//for each loop, insert the loop, create anonymous
//product type of objects, then add object here
//product list.add.file[n]
for ( String file : lineList){
//Class c2 = Class.forName(cn);
product_list.add(c1);
}
}
Solution 1:[1]
Assuming your Product class is like below:
class Product{
private String name;
//Constructor
public Product(String name) {
this.name=name;
}
}
then, your code shall look like
for ( String file : lineList){ //you have selected strange name for file line. should be product name
Product p = new Product(file);
product_list.add(p);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chetan Ahirrao |
