Sunday 15 June 2014

java - printf and reading text file -



java - printf and reading text file -

i'm having problems getting code sum of salaries of text file. it's supposed sum of them, give me average salary. i'm having troubles getting 1 of lines in output format correctly. please help, i'm still new programmimg! here main.

class="lang-java prettyprint-override">import java.util.*; import java.io.*; public class filein { public static void main(string[] args) { string fname; string lname; string rank; double salary = 0; double newsal = 0; scanner infile = null; employee[] emp = new employee[20]; int numemployee = 0; // open file seek { infile = new scanner(new file("employee.txt")); } grab (filenotfoundexception e) { system.err.println("error: file employee.txt not found "); } while (infile.hasnext()) { fname = infile.next(); lname = infile.next(); rank = infile.next(); newsal = infile.nextdouble(); salary += newsal; emp[numemployee] = new employee(fname, lname, rank, salary); numemployee++; } system.out.println("acme corporation \n"); system.out.printf("number of employees: %5d ", numemployee); system.out.printf("\naverage salary: %13.2f", salary / numemployee); system.out.printf("\nannual total %16.2f", salary ); system.out.printf("\n\n%-8s %15s %10s", "name", "rank", "salary\n"); (int = 0; < numemployee; i++) { system.out.printf("%s, %s \t%7s %11.2f\n", emp[i].getlname(), emp[i].getfname(), emp[i].getrank(), emp[i].getsalary()); } infile.close(); } }

and here sample output:

class="lang-none prettyprint-override">acme corporation number of employees: 9 average salary: 58740.50 annual total 528664.54 name rank salary ------------------ ---- --------- jones, william b2 42500.00 baker, susan a3 107500.00 caine, horatio a1 191268.95 baer, teddy b4 244268.95 gator, allie a2 292268.95 mander, sally a1 354392.84 aspargus, amy a1 454442.84 huckleberry, henry b1 495677.34 rutabaga, ryan b2 528664.54

the average salary , annual total wrong, total should on 2 1000000 , average salary supposed approximately 300k. give thanks you!!

it looks assigning cummulative salary every new employee-input:

emp[numemployee] = new employee(fname, lname, rank, salary);

shouldn't be?

emp[numemployee] = new employee(fname, lname, rank, newsal);

java printf

No comments:

Post a Comment