how do i read data from a data file into respective objects.!!1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blehhhhh
    New Member
    • May 2010
    • 13

    how do i read data from a data file into respective objects.!!1

    hi guys,
    So i have this java assignment where I have to create an employee class with given datatypes and have accessor methods in it. Along with that i have to create a text file(data file) with each employee's data on a single line for every detail. then in the main method, I have to create the 2 employee objects and then read all the employee data from the data file to the respective employee objects that's already created. Next i have to print all the data from the 3 employee objects and the average of their years of service. And the last thing, the difference between employee 1 and 2, 1 and 3, 2 and 3. My problem is, I have done the employee class with constructors and getters and setters and for the main class i created 1 employee object(for testing..once it works for 1, i'll add up the other 2). But I don't understannd how do i read from the data file to the main class and then pprint them. Here's the code

    Code:
    public class Employee 
    {
    	private String name;
    	private String id;
    	private double salary;
    	private String office;
    	private String extension;
    	private int years_of_service;
    	Employee(String n, String i_d, double sal, String off, String ext, int y_s) {
    	
    		name = n;
    		id = i_d;
    		salary = sal;
    		office = off;
    		extension = ext;
    		years_of_service = y_s;
    	}
    
    	public void setName(String n) 
    	{
    		name = n;
    	}
    
    	public String getName() 
    	{
    		return name;
    	}
    
    	public void setId(String i_d) 
    	{
    		id = i_d;
    	}
    
    	public String getId() 
    	{
    		return id;
    	}
    
    	public void setSalary(double sal) 
    	{
    		salary = sal;
    	}
    
    	public double getSalary() 
    	{
    		return salary;
    	}
    	public void setOffice(String off) 
    	{
    		office = off;
    	}
    
    	public String getOffice() 
    	{
    		return office;
    	}
    
    	public void setExtension(String ext) 
    	{
    		extension = ext;
    	}
    
    	public String getExtension() 
    	{
    		return extension;
    	}
    
    	public void setYears(int y_s) 
    	{
    		years_of_service = y_s;
    	}
    
    	public int getYears() 
    	{
    		return years_of_service;
    	}
    
    }
    the next is the data file (saved as name.txt)

    Code:
    
    Sally Smith
    345564
    4567.34
    HH 332
    322
    3
    Heena Patel
    434555
    463.55
    HH 545
    534
    3
    Kin Bleh
    674364
    6555.6
    HH 455
    345
    2
    The next is my main class file

    Code:
    import java.util.*;
    import java.io.*;
    public class EmpTest {
    	String na;
    	Scanner scan = new Scanner();
    	Employee e1 = new Employee();
    
    	na= scan.nextline();
    		e1.setName(na);
    	System.out println("name:" +e1.getName());
    	}
    But i don't think this is right..i'm not sure..here's what i get as error


    EmpTest.java:8: <identifier> expected
    na= scan.nextline() ;
    ^
    EmpTest.java:9: <identifier> expected
    e1.setName(na);
    ^
    EmpTest.java:9: <identifier> expected
    e1.setName(na);
    ^
    EmpTest.java:10 : illegal start of type
    System.out println("name:" +e1.getName());
    ^
    EmpTest.java:10 : ')' expected
    System.out println("name:" +e1.getName());
    ^
    EmpTest.java:10 : ';' expected
    System.out println("name:" +e1.getName());
    ^
    EmpTest.java:10 : illegal start of type
    System.out println("name:" +e1.getName());
    ^
    EmpTest.java:10 : ';' expected
    System.out println("name:" +e1.getName());
    ^
    8 errors



    Any help would be appreciated!!
    thanks
Working...