第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

用java讀取CSV文件并檢索處理所有n個(gè)案例的值

用java讀取CSV文件并檢索處理所有n個(gè)案例的值

慕村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 個(gè)字符串,分別對(duì)應(yīng)于name、priceperproduct和qty_sold。我必須處理所有可能的 n 種情況。我能做些什么?
查看完整描述

1 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以創(chuàng)建一個(gè)包含變量名稱、價(jià)格和數(shù)量的對(duì)象。然后,您可以分割每一行并將每個(gè)分隔的值存儲(chǔ)在之前創(chuàng)建的對(duì)象的數(shù)組中。


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

查看完整回答
反對(duì) 回復(fù) 2023-06-28
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)