d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Homework Help > Java - One Method Reading From File
Add Reply New Topic New Poll
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Sep 15 2014 01:55pm
I figured most of it out but I need help with one thing -

In main I have

Code
EmployeeDataReader employeeDataReader = new EmployeeDataReader("EmployeeData/EmployeeData.csv");
Employee[] employeeDatabase = employeeDataReader.getEmployeeData();
displayEmployees(employeeDatabase);


And in EmployeeDataReader I have:

Code
public Employee[] getEmployeeData(){
getFilePath();
/*Reads from the file stored in the attribute filePath. The file used is stored in
EmployeeData/EmployeeData.csv. A “csv” is a file structure that uses commas to
delimited values, thus the meaning comma separated values (csv). After you read a line
from the file use the split(“,”) string method, that will decompose the line into an array of
strings. Next you need to create a new Employee Object with the contents from the array of
strings returned from split and store that into an array of Employee Objects. An initialized
array of Employees will be returned to the caller.
The EmployeeData.csv file structure is: name,age,salary
Note: Check for overflow of your array, never trust that the number of lines in a file will be
the same size as your array! Make sure you use the int Integer.parseInt(String s) and
*/
try {

BufferedReader data = new BufferedReader(new FileReader(filePath));
while ((filePath = data.readLine()) != null) {

// use comma as separator
String[] dataz = line.split(",");
String name = dataz[0];
int age = Integer.parseInt(dataz[1]);
double salary = Double.parseDouble(dataz[2]);
}

return;
}


How do I create an object like this:
Next you need to create a new Employee Object with the contents from the array of strings returned from split and store that into an array of Employee Objects. An initialized array of Employees will be returned to the caller.
The EmployeeData.csv file structure is: name,age,salary

Just Employee = new Employee(name, age, salary); ??

This post was edited by HoneyBadger on Sep 15 2014 02:24pm
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Sep 15 2014 02:35pm
I just need to return an initialized array of employees but I'm not sure how to do this? Under double salary do I do Employee hello = new Employee (name, age, salary); ?? Then just return hello? But how do I do it as an array? Employee hello[]?
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Sep 15 2014 02:41pm
read through the file once to get the number of rows, then initialize the employee array with that size. Then use a counter to set the employees in the array to the new employee.

ex.

public Employee[] getEmployeeData(){
getFilePath();
/*Reads from the file stored in the attribute filePath. The file used is stored in
EmployeeData/EmployeeData.csv. A “csv” is a file structure that uses commas to
delimited values, thus the meaning comma separated values (csv). After you read a line
from the file use the split(“,”) string method, that will decompose the line into an array of
strings. Next you need to create a new Employee Object with the contents from the array of
strings returned from split and store that into an array of Employee Objects. An initialized
array of Employees will be returned to the caller.
The EmployeeData.csv file structure is: name,age,salary
Note: Check for overflow of your array, never trust that the number of lines in a file will be
the same size as your array! Make sure you use the int Integer.parseInt(String s) and
*/
try {


BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
String input;
int count = 0;
while((input = bufferedReader.readLine()) != null)
{
count++;
}
bufferedReader .close();
Employee[] employees = new Employee[count];


BufferedReader data = new BufferedReader(new FileReader(filePath));

int i = 0;
while ((filePath = data.readLine()) != null) {

// use comma as separator
String[] dataz = line.split(",");
String name = dataz[0];
int age = Integer.parseInt(dataz[1]);
double salary = Double.parseDouble(dataz[2]);
employees[i]=new Employee(name,age,salary);
i++;
}

return employees;
}

This post was edited by labatymo on Sep 15 2014 02:45pm
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Sep 15 2014 02:57pm
What is the purpose of having the first while to count how many lines there are? I'm just wondering the logic behind it!

Thank you very much! I'll give it a try once I get rid of this stupid try statement that is messing it all up.

I did a sys.out line to see the count and it shows count == 12 when there's only 11 lines in the file.

I'm getting an Array out of bounds exception.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at EmployeeDataReader.getEmployeeData(EmployeeDataReader.java:47)
at LabTwoDriver.main(LabTwoDriver.java:25)

LabTwoDriver 25 is where I call it all: Employee[] employeeDatabase = employeeDataReader.getEmployeeData();

Line 47 in EmployeeDataReader is where I parse the int: int age = Integer.parseInt(dataz[1]); Gives me index out of bounds??

e/ I'm not sure why it's throwing line 47 in DataReader I did system.out.print lines and it's printing the name/age correctly so I'm not exactly sure why there is an error.

This post was edited by HoneyBadger on Sep 15 2014 03:20pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 15 2014 07:42pm
Quote (HoneyBadger @ Sep 15 2014 04:57pm)
What is the purpose of having the first while to count how many lines there are? I'm just wondering the logic behind it! .


look at where it's used. arrays are fixed structures; you need to know how many to put in it before you initialize the array. if you skipped the first loop, how would you know how many fit in the array?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll