Thread: I keep getting this error index out of bounds

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    208

    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

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I will tell you I was getting a Input string not in correct format exception first
    number[i] = int.Parse(words[0]);
    but I changed it to a string number[i] = words[0];
    To fix the error, then it gave me the index out of bounds exception.
    if that helps any to tell you guys that

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how about you add debug print of word.size() before using word array?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Sorry I haven't replied sooner I been trying to research a little on your Idea but I guess I just don't understand how to do what your talking about

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is words length after line 88? Did you try to run your code in debugger?
    BTW new on line 83 has no meaning - you will immediately overwrite the allocated variable with result returned by Split
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Yeah I'm doing Problems out of a book I have but most of the code was written by our teacher when I was in college I never really understood ParseLinein() that well but I will bring it up to the teacher I have another program I will post that is similar but it works thats why I don't understand why it doesn't but the other code will compile and I"m very new to the debugger

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Code:
    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    
    namespace Chapter5Problem8
    {
        class Program
        {
            const string INPUT_FILE = "\\Projects\\Chapter5Problem8\\Chapter5Problem8\\Update.txt";
            const string OUTPUT_FILE = "\\Projects\\Chapter5Problem8\\Chapter5Problem8\\output.txt";
    
            static uint employees = 7;
            static double totalPay = 0;
    
            static int[] num             = new int[employees + 1];
            static string[] name         = new string[employees + 1];
            static double[] hourly       = new double[employees + 1];
            static double[] regular      = new double[employees + 1];
            static double[] overtime     = new double[employees + 1];
            static double[] regPay       = new double[employees + 1];
            static double[] overTimePay  = new double[employees + 1];
            static double[] total        = new double[employees + 1];
            
    
            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(' ');
                num[i] = int.Parse(words[0]);
                name[i] = words[1];
                hourly[i] = double.Parse(words[2]);
                regular[i] = double.Parse(words[3]);
                overtime[i] = double.Parse(words[4]);
    
            }
    
            static void Total()
            {
                int i;
                for (i = 0; i <= employees; i++)
                {
                    regPay[i] = regular[i] * hourly[i];
                    overTimePay[i] = overtime[i] * hourly[i] * 1.5F;
                    total[i] = (double) Math.Round(regPay[i] + overTimePay[i],2);
                    totalPay += total[i];
                }
            }
    
            static void Display()
            {
                int i;
    
                fileOut.WriteLine("#          Name           Reg Hours       Overtime        Rate        total");
                fileOut.WriteLine("------------------------------------------------------------------------------");
                for (i = 0; i <= employees - 1; i++)
                {
                    fileOut.WriteLine("{0}          {1,-8}       {2,-10}      {3,-10}      {4,-6:C}      {5:C}", num[i], name[i], regular[i], overtime[i], hourly[i], total[i]);
                }
                fileOut.WriteLine("-------------------------------------------------------------------------------");
                fileOut.WriteLine("                                                                      {0:C}",totalPay);
            }
    
            static void CloseFiles()
            {
                fileIn.Close();
                fileOut.Close();
            }
        }
    }
    Here's code from my other program that works


    here's the text

    1 Smith 7.50 38.41 0
    2 Scoggins 11.00 40.00 5.33
    3 Gray 5.00 33.87 0
    4 Ensor 9.50 40.00 22.3
    5 Pinet 22.00 38.58 0
    6 Jones 4.55 28.00 0
    7 Chance 15.33 40.00 4.44



    here's the output

    output.txt

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    I guess the true error I been getting is Input string was not in correct format I'll post my code exactly the way I had it so this is a little different

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    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 int[] number         = new int[employees + 1];
            static double[] rate        = new double[employees + 1];
            static double[] hours       = new double[employees + 1];
            static double[] gross       = new double[employees + 1];
            static double[] netPay      = new double[employees + 1];
            static double[] sum         = new double[employees + 1];
            static double[] average     = new double[employees + 1];
    
    
            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] = int.Parse(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 - 1; 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();
            }
        }
    }

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Chapter 5 Problem 8 will compile yet I get an error INPUT STRING WAS NOT IN CORRECT FORMAT error with Chapter 5 Problem 9 They seem to be exactly the same almost so I don't know

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    208
    Well I figured out the error myself it was simply my text file was corrupt go figure I guess you have to be real careful when making text files because they can mess up

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by artistunknown View Post
    Well I figured out the error myself it was simply my text file was corrupt go figure I guess you have to be real careful when making text files because they can mess up
    Or you write a code in such a way that if the corrupted line is encountered - you just skip it and continue reading file
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. index out of bounds error
    By artistunknown in forum C# Programming
    Replies: 5
    Last Post: 06-27-2014, 11:25 PM
  2. Replies: 5
    Last Post: 01-16-2012, 02:22 PM
  3. Replies: 1
    Last Post: 08-12-2011, 03:07 AM
  4. file index update error
    By daluu in forum C Programming
    Replies: 1
    Last Post: 04-28-2003, 02:47 AM
  5. illegal vector index and checking bounds
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-23-2001, 11:36 AM