用java读取CSV文件并检索处理所有n个案例的值

name,priceperproduct,qty_sold

"Pollen's - Weeds, Weed mix 2630",72,117

Losartan Potassium,46,532

INSTANT HAND SANITIZER,65,594

"Sodium Sulfacetamide, Sulfur",45,359`

我必须从每行获取 3 个字符串,分别对应于name、priceperproduct和qty_sold。


我必须处理所有可能的 n 种情况。我能做些什么?


慕村9548890
浏览 99回答 1
1回答

绝地无双

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

相关分类

Java