为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕村9548890 2023-06-28 15:46:27
name,priceperproduct,qty_sold"Pollen's - Weeds, Weed mix 2630",72,117Losartan Potassium,46,532INSTANT HAND SANITIZER,65,594"Sodium Sulfacetamide, Sulfur",45,359`我必须从每行获取 3 个字符串,分别对应于name、priceperproduct和qty_sold。我必须处理所有可能的 n 种情况。我能做些什么?
查看完整描述

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());

    }

}


}

这将输出:

//img1.sycdn.imooc.com//649be5810001520c04610163.jpg

查看完整回答
反对 回复 2023-06-28
  • 1 回答
  • 0 关注
  • 103 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信