I'm new to C# and I'm having some trouble getting input from a file and using it properly.
I need to get input from a file and insert the values given into a function but the problem is I need to give the functions parameters that aren't strings.
TimePiece is the class I created. It take two ushort variables as the parameters.
const int NUM_TIMEPIECES = 15;
TimePiece[] T = new TimePiece[NUM_TIMEPIECES];
try
{
using (StreamReader stream = new StreamReader("Times.txt"))
{
string line;
line = stream.ReadToEnd();
string[] lines = line.Split(' ');
ushort j = 0;
for (int i = 0; i < NUM_TIMEPIECES; ++i)
{
T[i] = new TimePiece(Convert.ToUInt16(lines[j]), Convert.ToUInt16(lines[++j]));
++j;
}
}
}
catch (Exception error)
{
Console.WriteLine("Error.");
Console.WriteLine(error.Message);
}
}
Input File text:
2400 1
1320 0
1259 0
0312 1
1527 1
0707 1
0033 0
1315 1
2222 0
1740 0
1645 1
0210 1
2342 1
1234 0
1652 0
I've tried various other methods to get this to work but it keeps giving me the error "Input string was not in correct format."
What am I doing wrong? And is there a better way to extract the data from the file and insert it into my function?
Thanks