Hello,
Im trying to read a CSV into a 2d array, where the rows of CSV are the rows of the array, and the columns are defined by the separating commas in the file. It will be a rectangular array.
any help would be ace!
thankys

Recommended Answers

All 3 Replies

You can read the file contents in line by line then use string.Split() to convert each row into a string array.
The following works provided that the array definitely has the same number of rows as columns:

string filePath = @"C:\Users\Dan\Documents\test.txt";
            StreamReader sr = new StreamReader(filePath);
            string[,] strArray = null;
            int Row = 0;
            while (!sr.EndOfStream)
            {
                string[] Line = sr.ReadLine().Split(',');
                if (Row == 0)
                {
                    strArray = new string[Line.Length, Line.Length];
                }
                for (int column=0; column<Line.Length; column++)
                {
                    strArray[Row, column] = Line[column];
                }
                Row++;
            }

String strArray[] = line.split(";");

String docName = strArray[0];
String gender = strArray[1];

it showing error

java.lang.ArrayIndexOutOfBoundsException: 1
at CVSToXML.main(CVSToXML.java:70)

I want to add contents values

english.addElement("pubDate").addText(gender);
english.addElement("dicription").addText(gender);
english.addElement("link").addText(gender);
please help me

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.