954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array Lists and 2 dimenstional Arrays

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.

mark522
Light Poster
49 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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,

williamrojas78
Junior Poster
111 posts since Jun 2005
Reputation Points: 23
Solved Threads: 10
 

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?

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 
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.

mark522
Light Poster
49 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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!

mark522
Light Poster
49 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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;
                }
            }
williamrojas78
Junior Poster
111 posts since Jun 2005
Reputation Points: 23
Solved Threads: 10
 

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.

mark522
Light Poster
49 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

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

williamrojas78
Junior Poster
111 posts since Jun 2005
Reputation Points: 23
Solved Threads: 10
 

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

mark522
Light Poster
49 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: