1 回答
TA贡献1946条经验 获得超4个赞
您可以创建一个包含变量名称、价格和数量的对象。然后,您可以分割每一行并将每个分隔的值存储在之前创建的对象的数组中。
Product.java
public class Product {
private String name;
private int price;
private int qty;
public Product(String name, int price, int qty) {
this.name = name;
this.price = price;
this.qty = qty;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
@Override
public String toString() {
return "Product{" + "name=" + name + ", price=" + price + ", qty=" + qty + '}';
}
}
GetProducts.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class GetProducts {
public static void main(String[] args) {
ArrayList<Product> products = new ArrayList<Product>();
String csvFile = "products.csv"; //path to your csv file
String line = "";
String headerLine;
int x = 0;
try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
while ((line = br.readLine()) != null) {
if (x==0) // ignore header line
{
headerLine = line;
}
else
{
// use comma as separator
String[] split = line.split(",");
//Some product names contain commas that are part of the name, so we split again using quotation marks
if (line.charAt(0) == '"')
{
String[] split2 = line.split("\"");
//here we retrieve the rest of the data after the last quotation mark
//careful, it will only work if there are quotation marks in the product name only
split = split2[split2.length - 1].split(",");
products.add(new Product(split2[1], Integer.parseInt(split[1]), Integer.parseInt(split[2])));
}
else
{
//Here we just split using commas if there are no quotation marks in the product name
products.add(new Product(split[0], Integer.parseInt(split[1]), Integer.parseInt(split[2])));
}
}
x++; // increment x;
}
} catch (IOException e) {
e.printStackTrace();
}
//Output all Product objects
for (Product product : products)
{
System.out.println(product);
}
//Output products names only
for (Product product: products)
{
System.out.println(product.getName());
}
}
}
这将输出:
添加回答
举报