c# - I need to initialize a 2D array by reading lines from a .txt file then splitting each individual line every time a semicolon shows up in each line -


here problem:

i have program (using console) prompts users data such:

name:

date:

birthday:

the user enters data, data recorded in .txt file, each line being 1 instance user entered data, each individual piece of data being separated semicolon. text file looks this:

john smith;7/14/2015;6/5/1980  jane doe;7/15/2015;3/4/1975 

what want take each line of text, split semicolon, , put of text in 1 2d array. here code have far:

public static void linesplitcreator() {     string[, ] portionsofalldata; //the 2d array     string[] linesofpatientdata = system.io.file.readalllines(@"c:\users\administrator.rahul-hp\desktop\test\dataofpatient.txt");     //read lines , put them array     foreach (string s in linesofpatientdata) //attempt @ initializing 2d array     {         (int = 0; < 5; i++)         {             portionsofalldata[i, ] = s.split(';');         }     } } 

how do this?

because i'm not sure how many elements split() produce, suggest using jagged array.

a jagged array declared like:

string[][] myjaggedarray; 

for code, make following changes , should go.

public static void linesplitcreator() {     string[] linesofpatientdata = system.io.file.readalllines(@"c:\users\administrator.rahul-hp\desktop\test\dataofpatient.txt");     // jagged array.  know how many rows have linesofpatientdata     string[][] portionsofalldata = new string[linesofpatientdata.length][]; // 2d array      // read lines , put them array     (int = 0; < linesofpatientdata.length; i++)     {         portionsofalldata[i] = linesofpatientdata[i].split(';');     } } 

or use list<string[]>

public static void linesplitcreator() {     string[] linesofpatientdata = system.io.file.readalllines(@"c:\users\administrator.rahul-hp\desktop\test\dataofpatient.txt");     list<string[]> portionsofalldata = new list<string[]>();      // read lines , put them array     foreach (string s in linesofpatientdata)     {         portionsofalldata.add(s.split(';'));     } } 

Comments