473,419 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. public class Employee 
  2. {
  3.     private String name;
  4.     private String id;
  5.     private double salary;
  6.     private String office;
  7.     private String extension;
  8.     private int years_of_service;
  9.     Employee(String n, String i_d, double sal, String off, String ext, int y_s) {
  10.  
  11.         name = n;
  12.         id = i_d;
  13.         salary = sal;
  14.         office = off;
  15.         extension = ext;
  16.         years_of_service = y_s;
  17.     }
  18.  
  19.     public void setName(String n) 
  20.     {
  21.         name = n;
  22.     }
  23.  
  24.     public String getName() 
  25.     {
  26.         return name;
  27.     }
  28.  
  29.     public void setId(String i_d) 
  30.     {
  31.         id = i_d;
  32.     }
  33.  
  34.     public String getId() 
  35.     {
  36.         return id;
  37.     }
  38.  
  39.     public void setSalary(double sal) 
  40.     {
  41.         salary = sal;
  42.     }
  43.  
  44.     public double getSalary() 
  45.     {
  46.         return salary;
  47.     }
  48.     public void setOffice(String off) 
  49.     {
  50.         office = off;
  51.     }
  52.  
  53.     public String getOffice() 
  54.     {
  55.         return office;
  56.     }
  57.  
  58.     public void setExtension(String ext) 
  59.     {
  60.         extension = ext;
  61.     }
  62.  
  63.     public String getExtension() 
  64.     {
  65.         return extension;
  66.     }
  67.  
  68.     public void setYears(int y_s) 
  69.     {
  70.         years_of_service = y_s;
  71.     }
  72.  
  73.     public int getYears() 
  74.     {
  75.         return years_of_service;
  76.     }
  77.  
  78. }
  79.  
  80.  
the next is the data file (saved as name.txt)

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Sally Smith
  4. 345564
  5. 4567.34
  6. HH 332
  7. 322
  8. 3
  9. Heena Patel
  10. 434555
  11. 463.55
  12. HH 545
  13. 534
  14. 3
  15. Kin Bleh
  16. 674364
  17. 6555.6
  18. HH 455
  19. 345
  20. 2
  21.  
The next is my main class file

Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.*;
  3. import java.io.*;
  4. public class EmpTest {
  5.     String na;
  6.     Scanner scan = new Scanner();
  7.     Employee e1 = new Employee();
  8.  
  9.     na= scan.nextline();
  10.         e1.setName(na);
  11.     System.out println("name:" +e1.getName());
  12.     }    
  13.  
  14.  
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
Sep 18 '10 #1
0 1219

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
3
by: tvn007 | last post by:
I wrote the code below to read data from file into structure using C. However, I would like to convert it to C++. Could someone please give me some hints. I am not that famaliar with C++ Thanks...
14
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
6
by: G.Esmeijer | last post by:
Friends, I would like to read a text file (fixed length formaated) really fast and store the data into an Access database (2003). Using the streamreader and reading line by line, separating the...
1
by: Julia | last post by:
Hi, I have basic questions regarding ASP.NET site I am going to use Microsoft application configuration block and I wonder 1.does caching read only data in the application can hurt ...
4
by: askar.bektassov | last post by:
Hello all, please correct me, if I do... from ClientForm import ParseResponse from urllib2 import urlopen .... response=urlopen(url) forms=ParseResponse(response) I won't be able to
3
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). ...
1
by: kgk | last post by:
I would like to concatenate several file-like objects to create a single file-like object. I've looked at fileinput, however this returns a fileinput object that is not very file-like. ...
1
by: Steven D'Aprano | last post by:
I have a proxy class that wraps an arbitrary file-like object fp and reads blocks of data from it. Is it safe to assume that fp.read(-1) will read until EOF? I know that's true for file.read() and...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.