Link to home
Start Free TrialLog in
Avatar of Sat80
Sat80

asked on

add text file to int?

Hi Experts,

I have this int:
            int[][] s =
        {

            new[] { 1, 4 },
            new[] { 1, 3},
 
        };

As you can see I have to specify the int elements, but I want to read a text file and add their lines as elements in that int. The text I have looks like this:
data.txt:
1,4
1,3

I would like to read the data.txt file and add their lines to ( int[][] s)?
Note: I am using C# 2008

Thanks for your help
Regards
Sat80
SOLUTION
Avatar of s_chilkury
s_chilkury
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, here is my solution. I suggest that all lines in the text file have two entries only. At least - same number of entries. Otherwise everything will be a bit more complicated. Have a look:

using System.IO;// don't forget the line


        private void button2_Click(object sender, EventArgs e)
        {

            try
            {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("TextFile1.txt"))
                {

                    List<int[]> listOfArrays = new List<int[]>();
                    String line;
                    // Read and display lines from the file until the end of
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] sar = line.Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
                        int[] intArr = new int[2];
                        // if we have only 2 elements - can do like :
                        intArr[0] = int.Parse(sar[0]);
                        intArr[1] = int.Parse(sar[1]);
                        listOfArrays.Add(intArr);
                    }
                    int[][] intArrayOfArrays = listOfArrays.ToArray();

                    for (int i = 0; i < intArrayOfArrays.Length; i++)
                    {
                        int[] intAr = intArrayOfArrays[i];
                        for (int j = 0; j < intAr.Length; j++)
                        {
                            System.Diagnostics.Debug.Write(intAr[j].ToString() + " - ");
                        }
                        System.Diagnostics.Debug.WriteLine("");

                    }
                }
            }

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just a note:

The asker wanted this type of an array:

 int[][] s

Really, this is an array of arrays. For integers, BTW.


s_chilkury proposes to use two-dimensional String[,] array2D; (of strings!). It's a bit different..

And yes, I put some parsing in my code but didn't much care about error handling. really, I always use

int.TryParse method...instead of just Parse...

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial