【发布时间】:2015-02-23 16:54:58
【问题描述】:
我被困住了。我不知道如何从文件中读取数据值到类对象。我必须为 Employee 类的三个引用做这件事。这是我目前所拥有的:
public void displayEmployees(View view)
{
EditText edt1;
TextView tv;
String urlfile; //For variable that stores the user input for url address
edt1 = (EditText) findViewById(R.id.edit_file);
tv = (TextView) findViewById(R.id.text_main);
//Declare references to class Employee
Employee e1;
Employee e2;
Employee e3;
//Create three objects for class Employee
e1 = new Employee();
e2 = new Employee();
e3 = new Employee();
//Retrieve what user has input
urlfile = edt1.getText().toString();
//Open and read the data values from a file on the web
try
{
//Create URL object
URL file_url = new URL(urlfile);
//try to open the file from the web
Scanner fsc = new Scanner(file_url.openStream());
}
catch(IOException e)
{
tv.setText("Error: Invalid URL Address or File Does Not Exist");
}
}
而且,这就是类的样子:
public class Employee {
private String name;
private String id;
private double salary;
private String office;
private String extension;
private int yearsofservice;
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public String getId()
{
return id;
}
public void setId(String ID)
{
id = ID;
}
public double getSalary()
{
return salary;
}
public void setSalary(double Salary)
{
salary = Salary;
}
public String getOffice()
{
return office;
}
public void setOffice(String Office)
{
office = Office;
}
public String getExt()
{
return extension;
}
public void setExt(String Extension)
{
extension = Extension;
}
public int getYearsOfServ()
{
return yearsofservice;
}
public void setYearsOfServ(int YearsOfService)
{
yearsofservice = YearsOfService;
}
}// end class Employee
最后,我正在读取的文件如下所示,例如:
乔·鲍勃 00001 9000.00 校园大厅 9999 5
【问题讨论】: