Advertisement
daniel_lawson9999

Daniel Lawson Hours Worked Chapter 6

May 2nd, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. /**
  2.  * Created by Daniel on 5/1/2016.
  3.  */
  4. /*
  5. context of hours.txt (:
  6. --------------------------
  7. 123 Kim 12.5 8.1 7.6 3.2
  8. 456 Eric 4.0 11.6 6.5 2.7 12
  9. 789 Stef 8.0 8.0 8.0 8.0 7.5
  10. ---------------------------
  11. Projected output:
  12. Kim (ID#123) worked 31.4 hours (7.85 hours/day)
  13. Eric (ID#456) worked 36.8 hours (7.36 hours/day)
  14. Stef (ID789) worked 39.5 hours (7.9 hours/day)
  15.  
  16.  */
  17. import java.util.*;
  18. import java.io.*;
  19. public class hoursWorked {
  20.     public static void main(String[] args)throws FileNotFoundException{
  21.         File h = new File("hours.txt");
  22.         Scanner s = new Scanner(h);
  23.         while(s.hasNextLine()){
  24.             String l = s.nextLine();
  25.             line(l);
  26.         }
  27.     }
  28.     public static void line(String l){
  29.         Scanner s = new Scanner(l);
  30.         int id = s.nextInt();
  31.         String name = s.next();
  32.         double sum = 0;
  33.         int c = 0;
  34.         while(s.hasNextDouble()){
  35.             sum+= s.nextDouble();
  36.             c++;
  37.         }
  38.         System.out.printf("%s (ID#%d) worked for %.1f hours (%.2f hours/day)\n",name,id,sum,sum/c);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement