473,666 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 1237

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

Similar topics

5
9616
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 all the contents of f2 will be created and thrown away. In the case of two StringIO objects, this means there's a point when the contents is held in memory three times.
3
2912
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 in advance ///////////////////////////////////////////////////////////////////////////////////////////////////////////// struct test { char enabled,
14
5830
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 want to store data like a database: ---------------------------------- Custumer:
5
2921
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 shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
6
23764
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 line into string just takes to long. When I Import the file with access manually goes fast. But how to use this fromout a C# programme who has done this before and who can give met some answers
1
1343
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 performance when multiple request arrived? (multiple pages read the cached data) 2.Instead of caching the data inside the Application dictionary can I use a
4
4578
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
3249
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 ). Please suggest me which is the best way of handling the file data. I- Method: ---------------- Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.
1
2071
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. something like # a has 50 bytes, and b has 100 bytes f = FileList (open('a'), open('b')) f.read (100) # read 50 bytes from a and 50 from b
1
1150
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 StringIO.read(), but is it a reasonable assumption to make for arbitrary file-like objects? To put it in more concrete terms, I have a class like this: class C(object): # Much simplified version. def __init__(self, fp):
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8785
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8560
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.