简体   繁体   中英

How to create a 2d array from a CSV file

I'm having trouble creating the 2-d array "smartdata" from my CSV file data "smart eye data.csv". I keep getting errors stating "Object reference not set to an instance of an object".

I know that 2 for loops will be necessary to create the outer and inner dimensions of the matrix, but still haven't got this to work. The CSV data is just a spreadsheet of numbers. Any help would be greatly appreciated. Thanks

using (StreamReader oStreamReader = new StreamReader(File.OpenRead("Smart Eye data.csv")))
    {
        sFileContents = oStreamReader.ReadToEnd();
    }

    string[][] smartdata = new string[1000][]; 

    string[] sFileLines = sFileContents.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

    int i = 0;

    foreach(string sFileline in sFileLines)
    {
        string[] rowarray = sFileline.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);


        for (int j = 0; j < rowarray.Length; j++)
        {

            smartdata[i][j] =rowarray[j]; //where the error occurs
            //Debug.Log(smartdata[i][j]);

                }
        i = i + 1 ;
    }

if you insist on using a 2d array (I would not) you simply need (and insist on not using csvhelper)

   foreach(string sFileline in sFileLines)
    {

 smartdata[i] = sFileline.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
}

if you want to do it the hard way then do this

    for (int j = 0; j < rowarray.Length; j++)
    {
        smartdata[i] = new string[rowarray.Length];
        smartdata[i][j] =rowarray[j]; //where the error occurs
        //Debug.Log(smartdata[i][j]);

            }
    i = i + 1 ;

Now you can see what my original comment meant. I a jagged array(what you have ) you have to allocate each row.

pm100 gave you the real problem: you're not allocating your inner array; hence the error.

Using a CSV library isn't a bad idea - but it certainly isn't a necessity.

Using lists (instead of arrays) has the benefit that you don't need to know the #/rows or #/columns in advance.

Here's an example:

List<List<string>> mylist = new List<List<string>>();
using (StreamReader sr = new StreamReader(File.OpenRead("Smart Eye data.csv")))
{
   string line;
   while((line = sr.ReadLine()) != null)  
   {  
      System.Console.WriteLine(line);  
      List<string>row = line.Split(",").ToList();
      mylist.Add(row);
   }
}
...

You should initialize child array of 2d array:

foreach(string sFileline in sFileLines)
{
  string[] rowarray = sFileline.Split(",".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);

    smartdata[i]=new string[rowarray.Length];
    for (int j = 0; j < rowarray.Length; j++)
    {

        smartdata[i][j] =rowarray[j]; //where the error occurs
        //Debug.Log(smartdata[i][j]);

    }
    i = i + 1 ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM