Why I get an array out of bounds error

artistunknown

New member
Joined
Dec 15, 2011
Messages
4
Programming Experience
1-3
can't figure this out

I get the error under addressNumber
I mean Index out of bounds error


C#:
using System;
using System.IO;
using System.Text.RegularExpressions;


   class Employee
   {

       public uint zipCode;
       public uint customerNumber;
       public string lastName;
       public string firstName;
       public uint addressNumber;
       public string addressName;
       public string addressName2;
       public string city;
       public string state;
    

       public Employee()
       {
           this.lastName = "";
           this.firstName = "";
           this.customerNumber = 0;
           this.addressNumber = 0;
           this.addressName = "";
           this.addressName2 = "";
           this.city = "";
           this.state = "";
           this.zipCode = 0;
       }

       public string LastName
       {
           get
           {
               return this.lastName;
           }
       }

       public string FirstName
       {
           get
           {
               return this.firstName;
           }
       }

       public uint CustomerNumber
       {
           get
           {
               return this.customerNumber;
           }
       }

       public uint AddressNumber
       {
           get
           {
               return this.addressNumber;
           }
       }

       public string AddressName
       {
           get
           {
               return this.addressName;
           }
       }

       public string AddressName2
       {
           get
           {
               return this.addressName2;
           }
       }


       public string City
       {
           get
           {
               return this.city;
           }
       }

       public string State 
       {
           get 
           {
               return this.state; 
           }
       }

       public uint ZipCode
       {
           get
           {
               return this.zipCode;
           }
       }

      
   
       static public Employee Parse( string stringValue )
        {
            string[] words;
            Employee tempEmployee = new Employee();

            stringValue = stringValue.Trim();
            while(Regex.IsMatch(stringValue,"[ ]{2}"))
                stringValue = stringValue.Replace("  "," ");
            words = stringValue.Split(' ');

            tempEmployee.customerNumber = UInt32.Parse(words[0]);
            tempEmployee.lastName  = words[1];
            tempEmployee.firstName = words[2];
            tempEmployee.addressNumber  = UInt32.Parse(words[3]);   //this is where the error is 
            tempEmployee.addressName = words[4];
            tempEmployee.addressName2 = words[5];
            tempEmployee.city = words[6];
            tempEmployee.state = words[7];
            tempEmployee.zipCode = UInt32.Parse(words[8]);

            return tempEmployee;
        }   
    }

    class Report
    {

        const string INPUTFILE = "\\MyProjects\\GladRagsCompany\\GladRagsCompany\\master.txt";
        const string OUTPUTFILE = "\\MyProjects\\GladRagsCompany\\GladRagsCompany\\Report.txt";

        public static int pageCount = 1;
        static int numOfEmployees;
     
        static int maxDetailLines = 30;

        static Employee[] EmployeeArray = new Employee[100];
        static StreamReader fileIn;
        static StreamWriter fileOut;


        static void Main()
        {
            if (OpenFiles())
            {
                InputData();
                PrintDetailLine();
                CloseFiles();
            }
            Console.ReadKey();
        }

        static bool OpenFiles()
        {
            bool sucessOpeningFiles = true;

            if (File.Exists(INPUTFILE))
            {
                fileIn = File.OpenText(INPUTFILE);
                Console.WriteLine("{0} was opened", INPUTFILE);
            }
            else 
            {
                Console.WriteLine("Error: {0} does not exist\n", INPUTFILE);
                sucessOpeningFiles = false;
            }
            fileOut = File.CreateText(OUTPUTFILE);
            if (File.Exists(OUTPUTFILE))
                Console.WriteLine("{0} was created\n", OUTPUTFILE);
            else
            {
                Console.WriteLine("Error: {0} could not be created\n", OUTPUTFILE);
                sucessOpeningFiles = false;
            }
            return sucessOpeningFiles;
        }

 
        static void InputData()
        {
            int i;
            string lineIn;

            i = 0;
            while ((lineIn = fileIn.ReadLine()) != null)
            {
                i++;
                EmployeeArray[i] = Employee.Parse(lineIn);
               
            }
            numOfEmployees = i;
        }

        static void PrintPageHeadings()
        {
            fileOut.WriteLine();
            fileOut.WriteLine("                                  GLAD RAGS CLOTHING COMPANY                                     ");
            fileOut.WriteLine("                           CURRENT ACCOUNT BALANCE                                                                    PAGE: {0}", pageCount);
            fileOut.WriteLine("CUSTOMER      CUSTOMER NAME                                    CUSTOMER ADDRESS                                       ACCOUNT BALANCE");
            fileOut.WriteLine("NUMBER");
            fileOut.WriteLine();
            pageCount++;
        }

        static void PrintDetailLine()
        {

            int i;
  
            PrintPageHeadings();

            for (i = 1; i <= maxDetailLines; i++)
                fileOut.WriteLine("{0,-13} {1} {2} {3} {4} {5} {6} {7} {8}", EmployeeArray[i].customerNumber, EmployeeArray[i].firstName, EmployeeArray[i].lastName,
                    EmployeeArray[i].addressNumber,EmployeeArray[i].addressName, EmployeeArray[i].addressName2, EmployeeArray[i].city, EmployeeArray[i].state, EmployeeArray[i].zipCode);

            PrintPageHeadings();

            for (i = maxDetailLines + 1; i <= numOfEmployees; i++)
                fileOut.WriteLine("{0,-13} {1} {2} {3} {4} {5} {6} {7} {8}", EmployeeArray[i].customerNumber, EmployeeArray[i].firstName, EmployeeArray[i].lastName,
                    EmployeeArray[i].addressNumber,EmployeeArray[i].addressName, EmployeeArray[i].addressName2, EmployeeArray[i].city, EmployeeArray[i].state, EmployeeArray[i].zipCode);
        }

        static void CloseFiles()
        {
            fileIn.Close(); fileOut.Close();
        }
   
}
Parse said:
tempEmployee.addressNumber = UInt32.Parse(words[3]); //this is where the error is
 
Last edited:
The conclusion is simple;) The split does only return 3 words (and words[3] is the 4th one). Difficult to say what goes wrong without (part of) a sample input file.

// OOPS: as a newbie, I'm reviving old threads :(
 
Last edited:
Back
Top Bottom