0

I am stuck. I don't know how to read a data value from a file to class object. I have to do it for three references for the class Employee. This is what I have so far as of now:

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

And, this is what the class looks like:

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

Lastly, the file that I am reading from look like this, for example:

Joe Bob 00001 9000.00 Campus Hall 9999 5

RĂ¼diger Herrmann
  • 20,512
  • 11
  • 62
  • 79

2 Answers2

0

I would rather use SQlite to persist the employee information.

for your problem you need to write your own parsing logic.

for reading check this question

Community
  • 1
  • 1
drulabs
  • 3,071
  • 28
  • 35
0

Follow these steps
1)Connect to data source.
2)In Source side make a Model class as getter and setter.
3)Create a Object of that class and Insert all Values using Setters.
4)And add that Object in the collections(List,Map,Set) if you are using(Make return object
5)On the Android app get that in same signature which is return type of Server side value setter.

6)In android site you can do something like suppose you are getting an ArrayList then

for(int i=0;i<yourArrayList.size();i++){
VarType var=yourArrayList.get(i).getFirstSetvalue();
VarType var2=yourArrayList.get(i).getSecondValue()
//do what you want

Hope this will help you

rogerwar
  • 238
  • 1
  • 8