Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by Daniel on 5/1/2016.
- */
- /*
- context of hours.txt (:
- --------------------------
- 123 Kim 12.5 8.1 7.6 3.2
- 456 Eric 4.0 11.6 6.5 2.7 12
- 789 Stef 8.0 8.0 8.0 8.0 7.5
- ---------------------------
- Projected output:
- Kim (ID#123) worked 31.4 hours (7.85 hours/day)
- Eric (ID#456) worked 36.8 hours (7.36 hours/day)
- Stef (ID789) worked 39.5 hours (7.9 hours/day)
- */
- import java.util.*;
- import java.io.*;
- public class hoursWorked {
- public static void main(String[] args)throws FileNotFoundException{
- File h = new File("hours.txt");
- Scanner s = new Scanner(h);
- while(s.hasNextLine()){
- String l = s.nextLine();
- line(l);
- }
- }
- public static void line(String l){
- Scanner s = new Scanner(l);
- int id = s.nextInt();
- String name = s.next();
- double sum = 0;
- int c = 0;
- while(s.hasNextDouble()){
- sum+= s.nextDouble();
- c++;
- }
- System.out.printf("%s (ID#%d) worked for %.1f hours (%.2f hours/day)\n",name,id,sum,sum/c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement