亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

    }

}


}

這將輸出:

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

查看完整回答
反對 回復 2023-06-28
  • 1 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號