java - Import data in file and use to compute salary -


working on java program computes salaries collection of employees of different types, imported txt file.

the program builds , runs if comment out:

system.out.println("\naverage salary 2014: " + (totalsalary / indexyear2014));  system.out.println("\naverage salary 2015: " + (totalsalary / indexyear2015)); 

however, not seem data imported/used @ all.

here full code:

package computeemployeesalary;  /**  *  * @author jason snook  * course name: intermediate programming (cmis 242)  * assignment: project 1 - compute employee salary  */  /**  * program computes salaries of collection of employees of  * different types: employee, salesman, executive  *   * salary employee calculated multiplying monthly salary 12  *   * salary salesman takes base of employee, , adds commission,  * has cap of 20000  *   * salary executive takes base of employee, , adds bonus  * in event current stock more 100 */  // imports import java.util.scanner; import java.io.*;  /**  * class contains employee name , monthly salary  */ // employee class class employee {     // variable declaration     string employeename;     int monthlysalary = 0;      // employee constructor     public employee(string employeename, int monthlysalary) {         this.employeename = employeename;         this.monthlysalary = monthlysalary;     } // end employee constructor method      // calculate employee annual salary     public int annualsalary() {         return monthlysalary * 12;     } // end annualsalary method      // return employee name , annual salary string     @override // takes advantage compiler checking     public string tostring() {         return "employee name: " + employeename +                 " salary: $" + monthlysalary;     } // end tostring method } // end employee class  /**  * class contains salesman annual sales , extends employee class  */ // salesman subclass, extends employee class class salesman extends employee {     // variable declaration     int annualsales = 0;      // salesman constructor     public salesman(string employeename, int monthlysalary, int annualsales) {         super(employeename, monthlysalary);         this.annualsales = annualsales;     } // end salesman constructor method      @override // takes advantage compiler checking     // computes commission , annual salary     public int annualsalary() {         int commission = annualsales * 3 / 100;          // if commission greater 25000, set commission 25000         if (commission > 25000) {             commission = 25000;         } // emd comission if          return (monthlysalary * 12 + commission);     } // end annualsalary method      @override // takes advantage compiler checking     // displays output details string     public string tostring(){         return "employee name: " + employeename +                 " salary: $" + monthlysalary;     } // end tostring method } // end salesman class  /**  * class contains current stock price , extends employee class  */ // executive subclass, extends employee class class executive extends employee {     // variable declaration     int currentstockprice = 0;      // executive constructor     public executive(string employeename,              int monthlysalary, int currentstockprice) {         super(employeename, monthlysalary);         this.currentstockprice = currentstockprice;     } // end executive constructor method      @override // takes advantage compiler checking     // computes commission , annual salary     public int annualsalary() {         int executivebonus = 0;          // adds executive bonus if stock greater 100         if(currentstockprice > 100) {             executivebonus = 20000;         } // end executivebonus if          return (monthlysalary * 12 + executivebonus);     } // end annualsalary method      @override // takes advantage compiler checking     // displays output details string     public string tostring() {         return "employee name: " + employeename +                 " salary: $" + monthlysalary;     } // end tostring method } // end executive class  /**  * class computes salary based on input file , reports results  */ public class computeemployeesalary {     // main method     public static void main(string[] args) throws ioexception {     // import text file , compute salary increase          try {             // create file instance             java.io.file file = new java.io.file("employees.txt");              // create scanner object             scanner input = new scanner(file);              // create arrays 2014 , 2015             employee year2014[] = new employee[20];             employee year2015[] = new employee[20];              // create indexes 2014 , 2015 arrays             int indexyear2014 = 0, indexyear2015 = 0;              // read data file             while (input.hasnext()) {                 string year = input.next();                 string employeetitle = input.next();                 string employeename = input.next();                 int monthlysalary = input.nextint();                  // action if employee regular employee.                 if (employeetitle.equalsignorecase("employee")) {                     employee regularemployee = new employee(employeename,                              monthlysalary);                      if (year == "2014") {                         year2014[indexyear2014++] = regularemployee;                     } // end 2014 if                      if (year == "2015") {                         year2015[indexyear2015++] = regularemployee;                     } // end 2015 if                 } // end employee if                  // action if employee salesman.                 if (employeetitle.equalsignorecase("salesman")){                     int annualsales = input.nextint();                      salesman salesemployee = new salesman(employeename,                              monthlysalary, annualsales);                      if (year == "2014") {                         year2014[indexyear2014++] = salesemployee;                     } // end 2014 if                      if (year == "2015") {                         year2015[indexyear2015++] = salesemployee;                     } // end 2015 if                 } // end salesman if                  // action if employee executive.                 if (employeetitle.equalsignorecase("executive")) {                     int currentstockprice = input.nextint();                      executive executiveemployee = new executive(employeename,                     monthlysalary, currentstockprice);                      if (year == "2014") {                         year2014[indexyear2014++] = executiveemployee;                     } // end 2014 if                      if (year == "2015") {                         year2015[indexyear2015++] = executiveemployee;                     } // end 2015 if                 } // end executive if              }  // end while              // generate report 2014             int totalsalary = 0;              system.out.println("===== salary report 2014 =====");              (int = 0; < indexyear2014; i++) {                 system.out.print(year2014[i]);                 system.out.println(" annual salary: " +                         year2014[i].annualsalary());             } // end annualsalary 2014              (int = 0; < indexyear2014; i++) {                 totalsalary+= year2014[i].annualsalary();             } // end totalsalary 2014              system.out.println("\naverage salary 2014: " +                     (totalsalary / indexyear2014));              // generate report 2015             system.out.println("\n===== salary report 2015 =====");              (int = 0; < indexyear2015; i++) {                 system.out.print(year2015[i]);                 system.out.println(" annual salary: " +                         year2015[i].annualsalary());             } // end annualsalary 2015              (int = 0; < indexyear2015; i++) {                 totalsalary+= year2015[i].annualsalary();             }  // end totalsalary 2015              system.out.println("\naverage salary 2015: " +                     (totalsalary / indexyear2015));              // close input file             input.close();         } // end try         catch (ioexception i) {             system.out.println("error: file not found, error code: " + i);         } // end catch     } // end main method   } // end computeemployeesalary class 

any appreciated.

the reason works when comment out 2 lines cause divide 0 error. both indexyear2014 , indexyear2015 0.

the reason way you're comparing strings:

year == "2015" should year.equals("2015")


Comments