Advertisement
deyanmalinov

2. Create, Read and Write in Files

Apr 12th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.io.*;
  4.  
  5. public class Main {
  6.     private static final String FILE_PATH = "D://Coding/Java/Softuni/Java Fundamentals  2016/3. Java-Streams/users.txt";
  7.     private static final String SAVE_PATH = "D://Coding/Java/Softuni/Java Fundamentals  2016/3. Java-Streams/res.txt";
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.  
  11.  
  12.         BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));
  13.         BufferedWriter output = new BufferedWriter(new FileWriter(SAVE_PATH));
  14.  
  15.         String line;
  16.         while ((line = reader.readLine()) != null) {
  17.             String[] playerLine = line.split(" ");
  18.             String user = playerLine[0];
  19.             int minutes = 0;
  20.             int totalTime = 0;
  21.             for (int i = 1; i < playerLine.length; i++) {
  22.                 String column = playerLine[i];
  23.                 String[] logTime = column.split(":");
  24.                 int hours = Integer.parseInt(logTime[0]);
  25.                 int min = Integer.parseInt(logTime[1]);
  26.                 int totalMin = min +(hours * 60);
  27.                 minutes += totalMin;
  28.                 totalTime += totalMin;
  29.             }
  30.             int minInDay = 1440;
  31.             int days = minutes/ minInDay;
  32.             minutes %= minInDay;
  33.             int hours = minutes /60;
  34.             minutes %= 60;
  35.  
  36.             String outputFor = String.format("%s %d (%d days, %d hours, %d minutes)\r\n", user, totalTime, days, hours, minutes);
  37.             output.write(outputFor);
  38.         }
  39.         reader.close();
  40.         output.close();
  41.         }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement