I am trying to read text from a data file. Each line of text is shown below:
Group_5, 4911.66, 4910.274, 13781.725, 2018
I want to parse the string so that I can use the values for my calculations. The string array Str
is only collecting the first term Group_5
.
private void button1_Click(object sender, EventArgs e)
{
string Line;
string path = Survey_File.Text;
int i, j;
double X = 0, Y = 0, Z = 0, X_Last = 0.0, Y_Last = 0.0, Z_Last = 0.0, MD = 0, increment;
string[,] StrMatrix = new string[1000000, 3];
string[] Str = new string[5];
string[] Strings = new string[1000000];
string content;
i = j = 0;
StreamReader ReadSurveyFile = new StreamReader(Survey_File.Text);
while ((Line = Convert.ToString(ReadSurveyFile.ReadLine())) != null)
{
Line = Convert.ToString(Line);
Str = Line.Split(',', ',', ',', ',');
Strings[i] = Line;
Str = Strings[i].Split(',');
//X = Convert.ToDouble(Str[1]);
//Y = Convert.ToDouble(Str[2]);
//Z = Convert.ToDouble(Str[3]);
increment = Math.Sqrt((X - X_Last) * (X - X_Last) + (Y - Y_Last) * (Y - Y_Last) + (Z - Z_Last) * (Z - Z_Last));
X_Last = X;
Y_Last = Y;
Z_Last = Z;
MD = MD + increment;
i++;
Console.WriteLine(Str[1]); // Error Here
}
Console.ReadLine();
MessageBox.Show("Measured Depth = "+Convert.ToString(MD));
}
The error message that I receive is
"IndexOutOfRangeException was unhandled."
This doesn't make sense to me because I dimensionalized the string array Str
properly. The error appears where I specified above.