猿问

如何将 CSV 文件数据存储到 Java 中的数组中?

以下是我正在使用的CSV文件:


B00123,55

B00783,35

B00898,67

我需要读取并存储文件中输入的第一个值,例如B00123并将其存储在数组中。用户可以添加到文件中,使其不是固定数量的记录。


到目前为止,我已经尝试了这段代码:


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


    }


}

这会循环遍历文件,但不会不用任何数据填充数组。结果是:


****** Setup Array ******

[[D@42a57993

0.0 

0.0 

0.0 

0.0 

0.0 


慕尼黑8549860
浏览 206回答 3
3回答

GCT1015

实际上,当尝试将ID转换为Double时,在第一行中发生了一个NumberFormatException。所以我修改了这个程序,它对我有用。import java.io.BufferedReader;import java.io.FileReader;import java.util.HashMap;import java.util.Map;import java.util.Scanner;public class ArrayReader&nbsp;{&nbsp; &nbsp; static String xStrPath;&nbsp; &nbsp; static Map<String,Double> myArray = new HashMap<>();&nbsp; &nbsp; static void setUpMyCSVArray()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Scanner scanIn = null;&nbsp; &nbsp; &nbsp; &nbsp; int Rowc = 0;&nbsp; &nbsp; &nbsp; &nbsp; int Row = 0;&nbsp; &nbsp; &nbsp; &nbsp; int Colc = 0;&nbsp; &nbsp; &nbsp; &nbsp; int Col = 0;&nbsp; &nbsp; &nbsp; &nbsp; String InputLine = "";&nbsp; &nbsp; &nbsp; &nbsp; double xnum = 0;&nbsp; &nbsp; &nbsp; &nbsp; String xfileLocation;&nbsp; &nbsp; &nbsp; &nbsp; xfileLocation = "/Users/admin/Downloads/mark.txt";&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\n****** Setup Array ******");&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //setup a scanner&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*file reader uses xfileLocation data, BufferedRader uses&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file reader data and Scanner uses BufferedReader data*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanIn = new Scanner(new BufferedReader(new FileReader(xfileLocation)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (scanIn.hasNext())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //read line form file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputLine = scanIn.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //split the Inputline into an array at the comas&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] inArray = InputLine.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //copy the content of the inArray to the myArray&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myArray.put(inArray[0], Double.valueOf(inArray[1]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Increment the row in the Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rowc++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {System.out.println(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; printMyArray();&nbsp; &nbsp; }&nbsp; &nbsp; static void printMyArray()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //print the array&nbsp; &nbsp; &nbsp; &nbsp; for (String key : myArray.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(key + " = " + myArray.get(key));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; setUpMyCSVArray();&nbsp; &nbsp; }}&nbsp;输出:

www说

代码无法读取任何内容,您的文件路径不正确。给它绝对文件路径。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scanIn&nbsp;=&nbsp;new&nbsp;Scanner(new&nbsp;BufferedReader(new&nbsp;FileReader(xfileLocation)));

BIG阳

我使用opencsv库从csv中读取。import com.opencsv.CSVReader;public class CSV {&nbsp; &nbsp; private static String file = <filepath>;&nbsp; &nbsp; private static List<String> list = new ArrayList<>();&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; CSVReader reader = new CSVReader(new FileReader(file));&nbsp; &nbsp; &nbsp; &nbsp; String[] line;&nbsp; &nbsp; &nbsp; &nbsp; while ((line = reader.readNext()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(line[0]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Object[] myArray = list.toArray();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(myArray.length);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(myArray[0]);&nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出打印如下3B00123
随时随地看视频慕课网APP

相关分类

Java
我要回答