I keep getting this error index out of bounds
I was wondering if someone could help me figure out why my code keeps getting this index out of bounds exception. I will post the code and the text file. I just can't figure it out I've tried so many different things to get it to work
Code:
using System;
using System.IO;
using System.Text.RegularExpressions;
namespace Chapter5Problem9
{
class Program
{
const string INPUT_FILE = "\\Projects\\Chapter5Problem9\\Chapter5Problem9\\payRoll.txt";
const string OUTPUT_FILE = "\\Projects\\Chapter5Problem9\\Chapter5Problem9\\Out.txt";
static int employees = 7;
static string[] number = new string[employees];
static double[] rate = new double[employees];
static double[] hours = new double[employees];
static double[] gross = new double[employees];
static double[] netPay = new double[employees];
static double[] sum = new double[employees];
static double[] average = new double[employees];
static StreamReader fileIn;
static StreamWriter fileOut;
static void Main()
{
if (OpenFiles())
{
InputData();
Total();
Display();
CloseFiles();
}
}
static bool OpenFiles()
{
bool successOpeningFile = true;
if (File.Exists(INPUT_FILE))
{
fileIn = File.OpenText(INPUT_FILE);
Console.WriteLine("{0} was opened!", INPUT_FILE);
}
else
{
Console.WriteLine("Error: {0} does not exist!", INPUT_FILE);
successOpeningFile = false;
}
fileOut = File.CreateText(OUTPUT_FILE);
if (File.Exists(OUTPUT_FILE))
Console.WriteLine("{0} was created!", OUTPUT_FILE);
else
{
Console.WriteLine("Error: {0} could not be created!", OUTPUT_FILE);
successOpeningFile = false;
}
return successOpeningFile;
}
static void InputData()
{
uint i = 0;
string lineIn;
while ((lineIn = fileIn.ReadLine()) != null)
{
ParseLineIn(lineIn, i);
i++;
}
}
static void ParseLineIn(string lineIn, uint i)
{
string[] words = new string[2];
lineIn = lineIn.Trim();
while (Regex.IsMatch(lineIn, "[ ] {2}"))
lineIn = lineIn.Replace(" ", " ");
words = lineIn.Split(' ');
number[i] = words[0];
hours[i] = double.Parse(words[1]);
rate[i] = double.Parse(words[2]);
}
static void Total()
{
int i;
for (i = 0; i <= employees; i++)
{
if (hours[i] > 35)
gross[i] = hours[i] * rate[i] * 1.5F;
else
gross[i] = hours[i] * rate[i];
netPay[i] = gross[i] - (gross[i] * .15F);
sum[i] += netPay[i];
average[i] = sum[i] / employees;
}
}
static void Display()
{
int i;
fileOut.WriteLine("# Hours Rate Net Pay ");
fileOut.WriteLine("------------------------------------------------------------------------------");
for (i = 0; i < employees; i++)
{
fileOut.WriteLine("{0} {1} {2} {3:C}", number[i], hours[i], rate[i] , netPay[i]);
}
fileOut.WriteLine("-------------------------------------------------------------------------------");
fileOut.WriteLine(" Sum:{0:C} Average:{1:C} ", sum[i], average[i]);
}
static void CloseFiles()
{
fileIn.Close();
fileOut.Close();
}
}
}
here's the txt file
1 40.00 6.00
2 35.00 7.00
3 24.00 9.50
5 44.00 11.00
6 55.00 9.00
7 70.00 22.00