【问题标题】:How do I store the CSV file data into an array in Java?如何将 CSV 文件数据存储到 Java 中的数组中?
【发布时间】:2019-03-28 12:05:23
【问题描述】:

这是我正在使用的 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 

【问题讨论】:

  • 您好,您当然尝试过调试,对吧?将断点放入代码并运行调试器,这将帮助您了解正在发生的事情,或者从打印InputLine 值开始,例如。 ,顺便说一句,由于java约定,命名是驼峰式以小写开头,所以它应该命名为inputLine

标签: java arrays csv


【解决方案1】:

在尝试将 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 
{
    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();

    }

} 

输出:

【讨论】:

    【解决方案2】:

    代码无法读取任何内容,你的文件路径不正确。给它绝对文件路径。

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

    【讨论】:

      【解决方案3】:

      我使用 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 2018-06-30
        • 2017-11-04
        • 1970-01-01
        • 2019-03-03
        相关资源
        最近更新 更多