Not clear why would you use such primitive data format…
But the solution of the problem is simple: just write the code accurately. Use the debugger and accurate approaches. Your code shown many problems related to lack of neatness.
First, you don't dispose your instances of the
StreamReader
. This is the correct pattern:
using (StreamReader reader = new System.IO.StreamReader(filePaths[index])) {
string line = reader.ReadLine();
}
Makes sure you know all types implementing the interface
System.IDisposable
and make sure you call
Dispose
, via the
using
statement (not to be confused with
using
directive) or not:
https://msdn.microsoft.com/en-us/library/system.idisposable%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/yh598w02.aspx[
^].
Don't use variable/type/member names line <codex>x (I renamed it to <code>index.) Use semantically sensitive names, in correct English spelling, without abbreviations.
Don't hard-code
immediate constants like strings '\r', '\n', ' ', '='. Explicitly define constants or <code>readonly static members; in other cases, use the resources.
Now, if you read the whole file using
reader.ReadLine()
, you never need '\r', '\n'. Just know how your file is generated and use exact delimiters which can be there.
Two big mistakes are in the lines like
List1.Add(Convert.ToDouble(data[0]));
. Again, never use bad names line
List1
. Don't hard-code 0, 1, etc. Instead, use the number of element in the array returned by
string.Split
and use the loop. And finally, don't use
Convert.ToDouble
. This is what it is: parsing, not "conversion". Please understand that parsing can be successful or failed. Instead, use
double.TryParse
:
https://msdn.microsoft.com/en-us/library/system.double.tryparse%28v=vs.110%29.aspx[
^].
And so on…
When you rewrite your code in a neat way, if you still have any concerns about runtime, try to debug it using the debugger. Anyway, CodeProject questions on some exception without finding out what's going wrong under the debugger makes little sense…
—SA