xavi
xavi

Reputation: 433

Reading data from a file into an array of class objects

I'm reading data from a file that I need to be put into my array of objects (myEmployees). I believe my code is correct until the end of this example, but I am not sure how to read the data from the file, split it, and then put it correctly into my array of class objects.

//declare an array of employees
Employee[] myEmployees = new Employee[10];

//declare other variables
string inputLine;
string EmpName;
int EmpNum;
double EmpWage;
double EmpHours;
string EmpAdd;

//declare filepath
string environment = System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal) + "\\";

//get input
Console.Write("\nEnter a file name in My Documents: ");
string input = Console.ReadLine();
string path = environment + input;
Console.WriteLine("Opening the file...");

//read file
StreamReader myFile = new StreamReader(path);
inputLine = (myFile.ReadLine());

So I am reading data from a file that is structured like:

Employee Number
Employee Name
Employee Address
Employee wage Employee Hours

I need to read data from this file and parse it into the array of Employees that I've created. Here is the class data for class Employee:

public void Employeeconst ()
{
    employeeNum = 0;
    name = "";
    address = "";
    wage = 0.0;
    hours = 0.0;
}
public void SetEmployeeNum(int a)
{
    employeeNum = a;
}
public void SetName(string a)
{
    name = a;
}
public void SetAddress(string a)
{
    address = a;
}
public void SetWage(double a)
{
    wage = a;
}
public void SetHours(double a)
{
    hours = a;
}
public int GetEmployeeNum()
{
    return employeeNum;
}
public string GetName()
{
    return name;
}
public string GetAddress()
{
    return address;
}
public double GetWage()
{
    return wage;
}
public double GetHours()
{
    return hours;
}

Upvotes: 0

Views: 17255

Answers (2)

Ňuf
Ňuf

Reputation: 6217

Firstly, I would suggest to redesign your Employee class using properties, which is more readable and better follows principles of object oriented programming:

public class Employee
{
    public int EmployeeNum { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public double Wage { get; set; }
    public double Hours { get; set; }

    public void Employee()
    {
        EmployeeNum = 0;
        Name = "";
        Address = "";
        Wage = 0.0;
        Hours = 0.0;
    }
}

Also consider wrapping StreamReader in 'using' keyword, which ensures that file will be closed correctly. The rest of program is easy, just read your file line by line until end of file. Parse each line to desired type and set value to Employee object:

        using(StreamReader myFile = new StreamReader(path))
        {
            int index = 0;
            while(!myFile.EndOfStream)
            {
                Employee E = new Employee();
                E.EmployeeNum = Int32.Parse(myFile.ReadLine());
                E.Name = myFile.ReadLine();
                E.Address = myFile.ReadLine();
                E.Wage = Double.Parse(myFile.ReadLine());
                E.Hours = Double.Parse(myFile.ReadLine());
                myEmployees[index++] = E;
            }
        }

I did not include eny error checking in my sample code, so it is up to you to do it.

Upvotes: 3

tsells
tsells

Reputation: 2771

I would suggesting reading it line by line

See example here on MSDN

http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx

For each line you read you will have a string - you can split this string into an array using string.Split.

string mystring = "50305 FirstName LastName 1234 Anywhere Place 133.25 40";
string[] myarray = mystring.Split(' ');

I would suggest however handling the string input for doublespaces, etc.

You could do something like this to get rid of duplicate spaces.

string mynewstring = mystring.Replace("  ", " ");

while (mynewstring.Contains("  "))
{
  mynewstring = mystring.Replace("  ", " ");
}

Upvotes: 0

Related Questions