2012-04-16 87 views
0

我正在从需要放入我的对象数组(myEmployees)的文件中读取数据。我相信我的代码是正确的,直到这个例子结束,但我不知道如何从文件中读取数据,分割它,然后将它正确地放入我的类对象数组中。从文件读取数据到类对象数组中

//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()); 

从文件的结构类似于所以我读数据:

Employee Number 
Employee Name 
Employee Address 
Employee wage Employee Hours 

我需要从这个文件中读取数据,并将其解析到,我已经创建员工的阵列。下面是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; 
} 
+0

@Robert Harvey什么是删除我的答案的原因。请让我知道,这样我就不会重复这个错误。 – Sandeep 2012-04-16 01:49:14

回答

2

首先,我会建议重新设计Employee类使用性质,这是更具可读性和更好的遵循面向对象编程的原则:

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; 
    } 
} 

也可以考虑在“使用”包裹的StreamReader关键字,确保文件将被正确关闭。程序的其余部分很简单,只需逐行读取文件直到文件结束。分析每一行所需种类和设定值Employee对象:

 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; 
      } 
     } 

我并没有包括在我的示例代码ENY错误检查,所以它是由你来做到这一点。

+0

非常感谢,这是非常干净的代码!这是一项家庭作业,所以我实际上必须为每个get/set编写单独的方法,否则我会使用属性来完成它。我真的很喜欢你做文件阅读的方式。编辑:回答我自己的问题 – xavi 2012-04-16 06:08:49

+0

因此,此代码在“E.Wage”行上抛出异常,并说“输入字符串格式不正确”。 – xavi 2012-04-16 06:30:16

+0

既然你没有包括你的输入,文件的例子,我不知道问题是什么,但我猜你的文件中的工资(也可能是小时)使用小数点分隔符,与您的小数点分隔符不匹配文化。使用Parse方法的重载,它接受CultureInfo作为第二个参数,并提供适当的CultureInfo对象。像E.Wage = Double.Parse(myFile.ReadLine(),System.Globalization.CultureInfo.GetCultureInfo(“一些适当的文化”)); – 2012-04-16 07:34:12

0

我会建议由线

线看完见例如这里MSDN

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

对于每一个你看了,你将有行一个字符串 - 你可以使用string.Split将这个字符串分割成一个数组。

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

我建议然而对于处理doublespaces,字符串输入等

你可以做这样的事情来摆脱重复的空间。

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

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