Instantiate a class from text file
I have a text file with names, phone numbers, and employee ID's.
Example:
Chris Cotton
5555555555
CCOTTON
Joshua Trammell
5555555555
JTRAMMELL
And I have a class called Employee with a constructor that takes Name, Phone number, and ID.
How can I use that text file to instantiate the employee class for each record in my text file, using the employee id as the object name?
package com.evolution.model;
public class Employee {
private String name;
private int phoneNumber;
private String employeeId;
public Employee(String name, int phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
java file-io instantiation
add a comment |
I have a text file with names, phone numbers, and employee ID's.
Example:
Chris Cotton
5555555555
CCOTTON
Joshua Trammell
5555555555
JTRAMMELL
And I have a class called Employee with a constructor that takes Name, Phone number, and ID.
How can I use that text file to instantiate the employee class for each record in my text file, using the employee id as the object name?
package com.evolution.model;
public class Employee {
private String name;
private int phoneNumber;
private String employeeId;
public Employee(String name, int phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
java file-io instantiation
2
What have you tried so far?
– QBrute
Nov 19 '18 at 16:23
Break the problem down into testable parts. 1. Open up a file for reading. 2. Make sure you can figure out how to read a "record" as you have defined it, and can present this record as the right data for consumer code. 3. Write some code that takes three pieces of info and creates an Employee object. 4. Write some code that pulls all of this together to open the file, create the objects and do something with them, and then close the file. As it stands, this is a bit too broad for SO.
– jdv
Nov 19 '18 at 17:05
"using the employee id as the object name" You should really explain what you mean by this. It is unlikely you actually need to create an object that is referenced by a variable that is named after some data you fetch from a file. Maybe you mean you want to use the employee ID as a key of some sort for a database?
– jdv
Nov 19 '18 at 17:09
add a comment |
I have a text file with names, phone numbers, and employee ID's.
Example:
Chris Cotton
5555555555
CCOTTON
Joshua Trammell
5555555555
JTRAMMELL
And I have a class called Employee with a constructor that takes Name, Phone number, and ID.
How can I use that text file to instantiate the employee class for each record in my text file, using the employee id as the object name?
package com.evolution.model;
public class Employee {
private String name;
private int phoneNumber;
private String employeeId;
public Employee(String name, int phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
java file-io instantiation
I have a text file with names, phone numbers, and employee ID's.
Example:
Chris Cotton
5555555555
CCOTTON
Joshua Trammell
5555555555
JTRAMMELL
And I have a class called Employee with a constructor that takes Name, Phone number, and ID.
How can I use that text file to instantiate the employee class for each record in my text file, using the employee id as the object name?
package com.evolution.model;
public class Employee {
private String name;
private int phoneNumber;
private String employeeId;
public Employee(String name, int phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
java file-io instantiation
java file-io instantiation
edited Nov 19 '18 at 16:19
khelwood
30.9k74363
30.9k74363
asked Nov 19 '18 at 16:17
AmberlisiAmberlisi
91
91
2
What have you tried so far?
– QBrute
Nov 19 '18 at 16:23
Break the problem down into testable parts. 1. Open up a file for reading. 2. Make sure you can figure out how to read a "record" as you have defined it, and can present this record as the right data for consumer code. 3. Write some code that takes three pieces of info and creates an Employee object. 4. Write some code that pulls all of this together to open the file, create the objects and do something with them, and then close the file. As it stands, this is a bit too broad for SO.
– jdv
Nov 19 '18 at 17:05
"using the employee id as the object name" You should really explain what you mean by this. It is unlikely you actually need to create an object that is referenced by a variable that is named after some data you fetch from a file. Maybe you mean you want to use the employee ID as a key of some sort for a database?
– jdv
Nov 19 '18 at 17:09
add a comment |
2
What have you tried so far?
– QBrute
Nov 19 '18 at 16:23
Break the problem down into testable parts. 1. Open up a file for reading. 2. Make sure you can figure out how to read a "record" as you have defined it, and can present this record as the right data for consumer code. 3. Write some code that takes three pieces of info and creates an Employee object. 4. Write some code that pulls all of this together to open the file, create the objects and do something with them, and then close the file. As it stands, this is a bit too broad for SO.
– jdv
Nov 19 '18 at 17:05
"using the employee id as the object name" You should really explain what you mean by this. It is unlikely you actually need to create an object that is referenced by a variable that is named after some data you fetch from a file. Maybe you mean you want to use the employee ID as a key of some sort for a database?
– jdv
Nov 19 '18 at 17:09
2
2
What have you tried so far?
– QBrute
Nov 19 '18 at 16:23
What have you tried so far?
– QBrute
Nov 19 '18 at 16:23
Break the problem down into testable parts. 1. Open up a file for reading. 2. Make sure you can figure out how to read a "record" as you have defined it, and can present this record as the right data for consumer code. 3. Write some code that takes three pieces of info and creates an Employee object. 4. Write some code that pulls all of this together to open the file, create the objects and do something with them, and then close the file. As it stands, this is a bit too broad for SO.
– jdv
Nov 19 '18 at 17:05
Break the problem down into testable parts. 1. Open up a file for reading. 2. Make sure you can figure out how to read a "record" as you have defined it, and can present this record as the right data for consumer code. 3. Write some code that takes three pieces of info and creates an Employee object. 4. Write some code that pulls all of this together to open the file, create the objects and do something with them, and then close the file. As it stands, this is a bit too broad for SO.
– jdv
Nov 19 '18 at 17:05
"using the employee id as the object name" You should really explain what you mean by this. It is unlikely you actually need to create an object that is referenced by a variable that is named after some data you fetch from a file. Maybe you mean you want to use the employee ID as a key of some sort for a database?
– jdv
Nov 19 '18 at 17:09
"using the employee id as the object name" You should really explain what you mean by this. It is unlikely you actually need to create an object that is referenced by a variable that is named after some data you fetch from a file. Maybe you mean you want to use the employee ID as a key of some sort for a database?
– jdv
Nov 19 '18 at 17:09
add a comment |
1 Answer
1
active
oldest
votes
You can read the file line by line, and can maintain three consecutive lines in an array and the moment you capture three lines, you can construct the employee Object using its constructor the way I have shown in my main method.
Here is a sample code you can use. Just to be safe, I have changed data type for phoneNumber from int
to String
because a phone number may or may not contain only numbers. But if you are sure that your phone numbers will only contain digits that too not starting with zeroes, you can write Integer.parseInt(fileLines.get(i + 1))
instead of just fileLines.get(i + 1)
in the constructor creation call in for loop.
Here is a sample code for same,
public class Employee {
private String name;
private String phoneNumber;
private String employeeId;
public Employee(String name, String phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
public String toString() {
return String.format("name: %s, phoneNumber: %s, employeeId: %s",
new Object { name, phoneNumber, employeeId });
}
public static void main(String args) throws Exception {
try (BufferedReader reader = new BufferedReader(new FileReader("emp.txt"));) {
String line = null;
int counter = 0;
String lineObject = new String[3];
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() == 0) {
continue;
}
lineObject[counter++] = line;
if (counter == 3) {
counter = 0;
Employee employee = new Employee(lineObject[0], lineObject[1], lineObject[2]);
System.out.println(employee);
// employee.dowhatever();
}
}
}
}
}
When executed, this program prints following output,
name: Chris Cotton, phoneNumber: 5555555555, employeeId: CCOTTON
name: Joshua Trammell, phoneNumber: 5555555555, employeeId: JTRAMMELL
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378739%2finstantiate-a-class-from-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can read the file line by line, and can maintain three consecutive lines in an array and the moment you capture three lines, you can construct the employee Object using its constructor the way I have shown in my main method.
Here is a sample code you can use. Just to be safe, I have changed data type for phoneNumber from int
to String
because a phone number may or may not contain only numbers. But if you are sure that your phone numbers will only contain digits that too not starting with zeroes, you can write Integer.parseInt(fileLines.get(i + 1))
instead of just fileLines.get(i + 1)
in the constructor creation call in for loop.
Here is a sample code for same,
public class Employee {
private String name;
private String phoneNumber;
private String employeeId;
public Employee(String name, String phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
public String toString() {
return String.format("name: %s, phoneNumber: %s, employeeId: %s",
new Object { name, phoneNumber, employeeId });
}
public static void main(String args) throws Exception {
try (BufferedReader reader = new BufferedReader(new FileReader("emp.txt"));) {
String line = null;
int counter = 0;
String lineObject = new String[3];
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() == 0) {
continue;
}
lineObject[counter++] = line;
if (counter == 3) {
counter = 0;
Employee employee = new Employee(lineObject[0], lineObject[1], lineObject[2]);
System.out.println(employee);
// employee.dowhatever();
}
}
}
}
}
When executed, this program prints following output,
name: Chris Cotton, phoneNumber: 5555555555, employeeId: CCOTTON
name: Joshua Trammell, phoneNumber: 5555555555, employeeId: JTRAMMELL
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
|
show 7 more comments
You can read the file line by line, and can maintain three consecutive lines in an array and the moment you capture three lines, you can construct the employee Object using its constructor the way I have shown in my main method.
Here is a sample code you can use. Just to be safe, I have changed data type for phoneNumber from int
to String
because a phone number may or may not contain only numbers. But if you are sure that your phone numbers will only contain digits that too not starting with zeroes, you can write Integer.parseInt(fileLines.get(i + 1))
instead of just fileLines.get(i + 1)
in the constructor creation call in for loop.
Here is a sample code for same,
public class Employee {
private String name;
private String phoneNumber;
private String employeeId;
public Employee(String name, String phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
public String toString() {
return String.format("name: %s, phoneNumber: %s, employeeId: %s",
new Object { name, phoneNumber, employeeId });
}
public static void main(String args) throws Exception {
try (BufferedReader reader = new BufferedReader(new FileReader("emp.txt"));) {
String line = null;
int counter = 0;
String lineObject = new String[3];
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() == 0) {
continue;
}
lineObject[counter++] = line;
if (counter == 3) {
counter = 0;
Employee employee = new Employee(lineObject[0], lineObject[1], lineObject[2]);
System.out.println(employee);
// employee.dowhatever();
}
}
}
}
}
When executed, this program prints following output,
name: Chris Cotton, phoneNumber: 5555555555, employeeId: CCOTTON
name: Joshua Trammell, phoneNumber: 5555555555, employeeId: JTRAMMELL
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
|
show 7 more comments
You can read the file line by line, and can maintain three consecutive lines in an array and the moment you capture three lines, you can construct the employee Object using its constructor the way I have shown in my main method.
Here is a sample code you can use. Just to be safe, I have changed data type for phoneNumber from int
to String
because a phone number may or may not contain only numbers. But if you are sure that your phone numbers will only contain digits that too not starting with zeroes, you can write Integer.parseInt(fileLines.get(i + 1))
instead of just fileLines.get(i + 1)
in the constructor creation call in for loop.
Here is a sample code for same,
public class Employee {
private String name;
private String phoneNumber;
private String employeeId;
public Employee(String name, String phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
public String toString() {
return String.format("name: %s, phoneNumber: %s, employeeId: %s",
new Object { name, phoneNumber, employeeId });
}
public static void main(String args) throws Exception {
try (BufferedReader reader = new BufferedReader(new FileReader("emp.txt"));) {
String line = null;
int counter = 0;
String lineObject = new String[3];
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() == 0) {
continue;
}
lineObject[counter++] = line;
if (counter == 3) {
counter = 0;
Employee employee = new Employee(lineObject[0], lineObject[1], lineObject[2]);
System.out.println(employee);
// employee.dowhatever();
}
}
}
}
}
When executed, this program prints following output,
name: Chris Cotton, phoneNumber: 5555555555, employeeId: CCOTTON
name: Joshua Trammell, phoneNumber: 5555555555, employeeId: JTRAMMELL
You can read the file line by line, and can maintain three consecutive lines in an array and the moment you capture three lines, you can construct the employee Object using its constructor the way I have shown in my main method.
Here is a sample code you can use. Just to be safe, I have changed data type for phoneNumber from int
to String
because a phone number may or may not contain only numbers. But if you are sure that your phone numbers will only contain digits that too not starting with zeroes, you can write Integer.parseInt(fileLines.get(i + 1))
instead of just fileLines.get(i + 1)
in the constructor creation call in for loop.
Here is a sample code for same,
public class Employee {
private String name;
private String phoneNumber;
private String employeeId;
public Employee(String name, String phoneNumber, String employeeId) {
this.name = name;
this.phoneNumber = phoneNumber;
this.employeeId = employeeId;
}
public String toString() {
return String.format("name: %s, phoneNumber: %s, employeeId: %s",
new Object { name, phoneNumber, employeeId });
}
public static void main(String args) throws Exception {
try (BufferedReader reader = new BufferedReader(new FileReader("emp.txt"));) {
String line = null;
int counter = 0;
String lineObject = new String[3];
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() == 0) {
continue;
}
lineObject[counter++] = line;
if (counter == 3) {
counter = 0;
Employee employee = new Employee(lineObject[0], lineObject[1], lineObject[2]);
System.out.println(employee);
// employee.dowhatever();
}
}
}
}
}
When executed, this program prints following output,
name: Chris Cotton, phoneNumber: 5555555555, employeeId: CCOTTON
name: Joshua Trammell, phoneNumber: 5555555555, employeeId: JTRAMMELL
edited Nov 19 '18 at 21:19
answered Nov 19 '18 at 16:40
Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi
6,3362827
6,3362827
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
|
show 7 more comments
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
Reading the entire file into memory is both unnecessary and not scalable.
– VGR
Nov 19 '18 at 20:26
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: Well, I am aware about that which is why I tried using Files.lines() method which lazily reads the lines from file. But the information required to construct one object was lying in multiple lines so I didn't go for a relatively complex solution. Although of course I know how to avoid loading all file data into memory, but just didn't put much effort as OP hasn't mentioned anything about huge data files. Is that really a strong reason to downvote the answer?
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:04
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: I have updated my answer using plain old BufferedReader way of reading file. Hope this looks fine. However, I would love to learn from you, using Files.lines() method to read file and construct Employee object using those three parameters using stream and lambda expressions and avoiding storing data in memory. Thank You.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 21:25
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
@VGR: Still waiting for your reply :)
– Pushpesh Kumar Rajwanshi
Nov 20 '18 at 17:24
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
You are over-complicating things by reading only one line at a time. Why not just read one line, and if it’s not blank, immediately read two more lines, then create an Employee object from those three values?
– VGR
Nov 20 '18 at 17:56
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378739%2finstantiate-a-class-from-text-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What have you tried so far?
– QBrute
Nov 19 '18 at 16:23
Break the problem down into testable parts. 1. Open up a file for reading. 2. Make sure you can figure out how to read a "record" as you have defined it, and can present this record as the right data for consumer code. 3. Write some code that takes three pieces of info and creates an Employee object. 4. Write some code that pulls all of this together to open the file, create the objects and do something with them, and then close the file. As it stands, this is a bit too broad for SO.
– jdv
Nov 19 '18 at 17:05
"using the employee id as the object name" You should really explain what you mean by this. It is unlikely you actually need to create an object that is referenced by a variable that is named after some data you fetch from a file. Maybe you mean you want to use the employee ID as a key of some sort for a database?
– jdv
Nov 19 '18 at 17:09