Hi guys im having a lot of problems trying to code. I have opened a csv file and have read it using strings.I have splitted the strings by the ','.

I want to load each of these values into a 2 dimensional array! but im having problems. Ultimatly i want to load all the values into an array and then sum the rows and find the averages.

So far ive managed to get the values into an ArrayList. Ive tried converting the ArrayList to an Arry i keep runnig into problems. Any ideas or help at all would be helpful.Thank You

So far my code reads like this:

public static void Main(string[] args)
        {


            string line = System.IO.File.ReadAllText(@"C:\Users\40025634\Desktop\Project\TEST FILE.csv");

            ArrayList splitlinearray = new ArrayList(line.Split(','));
           


            Console.WriteLine("The ArrayList contains the following values:");
            PrintIndexAndValues(splitlinearray);

           }

        public static void PrintIndexAndValues(ArrayList splitlinearray)
        {
            int i = 0;
            foreach (Object o in splitlinearray)
                Console.WriteLine("\t{1}", i++, o);
            while (true) ;
        }
      }
    }

Thanks very much guys.

can you post a piece of the csv file?
aslo, when you say a two dimensional array, is it because there are 2 fields per row?

Regards,

Hi Mark522 welcome at DaniWeb!
Some remarks: line 22 loops forever, I think it is your intention to keep the console on the screen.
I use Console.ReadKey(); in debugmode for that.
Also you have one long list of data. How many columns you have?

Hi Mark522 welcome at DaniWeb!
Some remarks: line 22 loops forever, I think it is your intention to keep the console on the screen.
I use Console.ReadKey(); in debugmode for that.
Also you have one long list of data. How many columns you have?

Oh thanks! really new to the whole programming stuff.

can you post a piece of the csv file?
aslo, when you say a two dimensional array, is it because there are 2 fields per row?

Regards,

Its cause there are loads of columns and rows. I was told i would prob need a 2 dimenstional one. There are loads of values which i need to load into the array just once (i know this will take a bit of time)so for the purposes of testing it i created a smaller one in excel like this and saved it as a csv file:

Tr0 1 2 3 4 5
Tr1 1 2 3 4 5
Tr2 1 2 3 4 5

Thank you!

I think this will do what you want:
Hopefully it helps.

string file = System.IO.File.ReadAllText(".\\test.csv");
            
            ArrayList rows = new ArrayList(file.Split('\n'));

            foreach (string row in rows)
            {
                string r = row.Remove(row.Length -1, 1);

                ArrayList rowVals = new ArrayList(r.Split(','));

                double sum = 0;
                int addedItems = 0;

                foreach (string item in rowVals)
                {
                    try
                    {
                        int num = int.Parse(item);
                        sum += num;
                        addedItems++;
                    }
                    catch (Exception)
                    {
                        
                        //throw;
                    }
                }

                if (addedItems > 0)
                {
                    double avrg = sum / addedItems;
                }
            }

I think this will do what you want:
Hopefully it helps.

string file = System.IO.File.ReadAllText(".\\test.csv");
            
            ArrayList rows = new ArrayList(file.Split('\n'));

            foreach (string row in rows)
            {
                string r = row.Remove(row.Length -1, 1);

                ArrayList rowVals = new ArrayList(r.Split(','));

                double sum = 0;
                int addedItems = 0;

                foreach (string item in rowVals)
                {
                    try
                    {
                        int num = int.Parse(item);
                        sum += num;
                        addedItems++;
                    }
                    catch (Exception)
                    {
                        
                        //throw;
                    }
                }

                if (addedItems > 0)
                {
                    double avrg = sum / addedItems;
                }
            }

ThankYou its telling me that it cant start at a minus value " StartIndex cannot be less than zero.Parameter name: startIndex"

string r = row.Remove(row.Length -1, 1);

I tried changing it about a bit bt with no luck.

what the line does is that it removes the last character (which came as '\r').
if importing the file does not bring a CR at the end of the line, then you don't need it.

let me know

Its ok i got it thank you very much!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.