I have a file containing numbers of two digits.
10
20 25
30 45 60
I am reading the file line by line as strings and converting them into a string
array. Then, I am trying to insert them in an integer two dimensional list using int.Parse()
method but it's throwing an "Index out of range" exception.
What am I doing wrong?
public List<List<int>> num = new List<List<int>>();
int row = 0;
System.IO.StreamReader file = new System.IO.StreamReader("D:\\data.txt");
while (!file.EndOfStream)
{
string line = file.ReadLine();
string[] str = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < str.Length; i++)
{
num[row].Add(int.Parse(str[i]));
}
row++;
}