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

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

如何將 CSV 文件數(shù)據(jù)存儲到 Java 中的數(shù)組中?

如何將 CSV 文件數(shù)據(jù)存儲到 Java 中的數(shù)組中?

慕尼黑8549860 2022-09-07 21:01:59
以下是我正在使用的CSV文件:B00123,55B00783,35B00898,67我需要讀取并存儲文件中輸入的第一個(gè)值,例如B00123并將其存儲在數(shù)組中。用戶可以添加到文件中,使其不是固定數(shù)量的記錄。到目前為止,我已經(jīng)嘗試了這段代碼:public class ArrayReader {    static String xStrPath;    static double[][] myArray;    static void setUpMyCSVArray()    {        myArray = new double [4][5];        Scanner scanIn = null;        int Rowc = 0;        int Row = 0;        int Colc = 0;        int Col = 0;        String InputLine = "";        double xnum = 0;        String xfileLocation;        xfileLocation = "src\\marks.txt";        System.out.println("\n****** Setup Array ******");        try        {            //setup a scanner            /*file reader uses xfileLocation data, BufferedRader uses               file reader data and Scanner uses BufferedReader data*/            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));            while (scanIn.hasNext())            {                              //read line form file                InputLine = scanIn.nextLine();                //split the Inputline into an array at the comas                String[] InArray = InputLine.split(",");                //copy the content of the inArray to the myArray                for (int x = 0; x < myArray.length; x++)                {                    myArray[Rowc][x] = Double.parseDouble(InArray[x]);                }                //Increment the row in the Array                Rowc++;            }        }        catch(Exception e)        {        }        printMyArray();    }    static void printMyArray()    {        //print the array        for (int Rowc = 0; Rowc < 1; Rowc++)        {            for (int Colc = 0; Colc < 5; Colc++)            {                System.out.println(myArray[Rowc][Colc] + " ");            }            System.out.println();        }        return;    }    public static void main(String[] args)    {        setUpMyCSVArray();    }}這會循環(huán)遍歷文件,但不會不用任何數(shù)據(jù)填充數(shù)組。結(jié)果是:****** Setup Array ******[[D@42a579930.0 0.0 0.0 0.0 0.0 
查看完整描述

3 回答

?
GCT1015

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

實(shí)際上,當(dāng)嘗試將ID轉(zhuǎn)換為Double時(shí),在第一行中發(fā)生了一個(gè)NumberFormatException。所以我修改了這個(gè)程序,它對我有用。


import java.io.BufferedReader;

import java.io.FileReader;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;


public class ArrayReader 

{

    static String xStrPath;

    static Map<String,Double> myArray = new HashMap<>();


    static void setUpMyCSVArray()

    {


        Scanner scanIn = null;

        int Rowc = 0;

        int Row = 0;

        int Colc = 0;

        int Col = 0;

        String InputLine = "";

        double xnum = 0;

        String xfileLocation;


        xfileLocation = "/Users/admin/Downloads/mark.txt";


        System.out.println("\n****** Setup Array ******");


        try

        {

            //setup a scanner

            /*file reader uses xfileLocation data, BufferedRader uses 

              file reader data and Scanner uses BufferedReader data*/

            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));


            while (scanIn.hasNext())

            {              

                //read line form file

                InputLine = scanIn.nextLine();

                //split the Inputline into an array at the comas

                String[] inArray = InputLine.split(",");


                //copy the content of the inArray to the myArray

                    myArray.put(inArray[0], Double.valueOf(inArray[1]));

                //Increment the row in the Array

                Rowc++;

            }

        }

        catch(Exception e)

        {

System.out.println(e);

        }

        printMyArray();

    }


    static void printMyArray()

    {

        //print the array

        for (String key : myArray.keySet()) {

            System.out.println(key + " = " + myArray.get(key));

        }

        return;

    }


    public static void main(String[] args)

    {

        setUpMyCSVArray();


    }


輸出:

http://img1.sycdn.imooc.com//631896600001e1ea02680147.jpg

查看完整回答
反對 回復(fù) 2022-09-07
?
www說

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

代碼無法讀取任何內(nèi)容,您的文件路徑不正確。給它絕對文件路徑。

            scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));


查看完整回答
反對 回復(fù) 2022-09-07
?
BIG陽

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

我使用opencsv庫從csv中讀取。


import com.opencsv.CSVReader;


public class CSV {



    private static String file = <filepath>;


    private static List<String> list = new ArrayList<>();


    public static void main(String[] args) throws Exception {

      try {

        CSVReader reader = new CSVReader(new FileReader(file));

        String[] line;

        while ((line = reader.readNext()) != null) {

          list.add(line[0]);

        }

        Object[] myArray = list.toArray();

        System.out.println(myArray.length);

        System.out.println(myArray[0]);

      } catch (Exception e) {

        e.printStackTrace();

      }

    }

}

輸出打印如下


3

B00123


查看完整回答
反對 回復(fù) 2022-09-07
  • 3 回答
  • 0 關(guān)注
  • 223 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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