Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:25:33+00:00 2026-06-01T21:25:33+00:00

I’m reading data from a file that I need to be put into my

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:25:35+00:00Added an answer on June 1, 2026 at 9:25 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.