• Help with Java I/O
    2 replies, posted
I was getting help with this from a friend but i fear he may have steered me in the wrong direction. I used to be more of a C++ kinda girl and knew some COBOL but this is giving me a headache. You have been asked to produce a department report from an employee file and a department file with the following format: employee.txt file: text file with the following fields separated by spaces: ________________________________________________ employee.txt file is as follows: 218 E 195 John Adams 25.50 10/15/1998 15 0 201 E 182 Richard Jones 28.25 06/01/2000 15 0 319 C 195 Sue Green 19.00 04/01/2002 10 1 363 C 182 James Atkins 21.5 05/15/2006 8 0 532 E 195 Mary Bacon 8.95 07/01/2009 5 1 612 E 115 Ali Gator 12.75 11/10/2007 5 0 682 E 195 Sally Mander 9.5 03/18/2008 4 1 424 E 182 Glenn Jackson 32.95 12/01/1996 21 0 405 C 115 Teddy Baer 15.24 02/28/2005 12 0 829 E 182 Hannah Martin 25.0 08/12/2003 18 0 ________________________________________________________________________ Fields Description: Employee ID 3 characters Employee/Contractor Indicator 1 character: ‘E’ for employee, ‘C’ for contractor Department Code 3 characters First Name maximum 20 characters Last Name maximum 15 characters Salary float single precision Hire Date string in the format mm/dd/yyyy Vacation Days integer Training integer __________________________________________________________________________ department.txt file: CSV (comma separated value) text file in which each line in the file represents a department. The comma delimited fields are: Department Code Department Name Employee Id of the manager for the department __________________________________________________________________________ department.txt file is as follows: 195,Sales,319 115,Marketing,405 182,Research,424 __________________________________________________________________________ The program should output a report listing the Employees by Department. For each department, provide the manager’s name, the number of employees, and the total number of vacation days. A sample output is as follows: Sales Department Manager: John Smith Staff size: 2 Vacation Days: 15 ID Employee Name Hire Date Salary Vacation Days 127 John Smith 10/12/1996 $ 80.50 10 382 Mary Brown 2/30/2001 $ 50.00 5 Marketing Department Manager: Sue Green Staff size: 1 Vacation Days: 15 ID Employee Name Hire Date Salary Vacation Days 234 Sue Green 12/12/1990 $180.50 15 _____________________________________________________________________ My Employee class is: .. i feel this part is right because it is just getters and setters. public class Employee { private int empID; private String empOrCont; private int depCode; private String fName; private String lName; private float sal; private String hDate; private int vDays; private int train; public int getEmpID() { return empID; } public void setEmpID(int empID) { this.empID = empID; } public String getEmpOrCont() { return empOrCont; } public void setEmpOrCont(String empOrCont) { this.empOrCont = empOrCont; } public int getDepCode() { return depCode; } public void setDepCode(int depCode) { this.depCode = depCode; } public String getfName() { return fName; } public void setfName(String fName) { this.fName = fName; } public String getlName() { return lName; } public void setlName(String lName) { this.lName = lName; } public float getSal() { return sal; } public void setSal(float sal) { this.sal = sal; } public String gethDate() { return hDate; } public void sethDate(String hDate) { this.hDate = hDate; } public int getvDays() { return vDays; } public void setvDays(int vDays) { this.vDays = vDays; } public int getTrain() { return train; } public void setTrain(int train) { this.train = train; } } ______________________________________________________________________ my department class is as follows: same concept with the getters and setters public class Department { private int departmentCode; private String departmentName; private int managerID; public int getDepartmentCode() { return departmentCode; } public void setDepartmentCode(int departmentCode) { this.departmentCode = departmentCode; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } public int getManagerID() { return managerID; } public void setManagerID(int managerID) { this.managerID = managerID; } } _________________________________________________________________________ this is where i am having trouble where my friend had previously helped me but i feel like he has steered me in the wrong direction so far, and if not i dont know how to make everything print from reading the text file. import java.io.*; import java.util.Scanner; public class DepartmentMain { private Scanner inputEmployeeRecord; private Scanner inputDepartmentRecord; public void openFile1() { try { inputEmployeeRecord = new Scanner(new File("employee.txt")); } catch(FileNotFoundException fileNotFoundException) { System.err.println("The file was not found"); System.exit(1); } } public void openFile2() { try { inputDepartmentRecord = new Scanner(new File("department.txt")); } catch(FileNotFoundException fileNotFoundException) { System.err.println("The file was not found"); System.exit(1); } } public Employee getEmployee() { Employee record = new Employee(); if(inputEmployeeRecord.hasNext()); { record.setEmpID(inputEmployeeRecord.nextInt()); record.setEmpOrCont(inputEmployeeRecord.next()); record.setDepCode(inputEmployeeRecord.nextInt()); record.setfName(inputEmployeeRecord.next()); record.setlName(inputEmployeeRecord.next()); record.setSal(inputEmployeeRecord.nextInt()); record.sethDate(inputEmployeeRecord.next()); record.setvDays(inputEmployeeRecord.nextInt()); record.setTrain(inputEmployeeRecord.nextInt()); } return record; } public Department getDepartment() { Department record = new Department(); while(inputDepartmentRecord.hasNext()); { record.setDepartmentCode(inputDepartmentRecord.nextInt()); record.setDepartmentName(inputDepartmentRecord.next()); record.setManagerID(inputDepartmentRecord.nextInt()); } return record; } } ______________________________________________________________________________ any little advice or tips would help thank you very much. This girl is so lost.
What I'd use when dealing with reading text input from file (and did use when I got a programming assignment similar to this) is the String Tokenize function to break each line into string tokens. [url=http://www.rgagnon.com/javadetails/java-0015.html]Some example here[/url] If I got this right, read in the entire file with a Scanner, then pick it apart using the tokenizer and assign the tokens to each variable as you go. Tokenize returns only strings so you you'll have to do int i = Integer.parseInt(string_token);
Sorry, you need to Log In to post a reply to this thread.