Advertisement
ssunlimited

Sleep Tracker Average With Text Input

Apr 29th, 2024
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class SleepAVG{
  5.    public static void main(String[] args){
  6.       try{
  7.       Scanner console = new Scanner(new File ("Sleep Tracker.txt"));
  8.       List<Integer> daysSleepTime = new ArrayList<Integer>();
  9.       while(console.hasNextInt()){
  10.       int hours = console.nextInt();
  11.       int minutes = console.nextInt();
  12.       int total = (hours * 60) + minutes;
  13.       daysSleepTime.add(total);
  14.       }
  15.       int total = 0;
  16.       for(int sleepTime : daysSleepTime) total += sleepTime;
  17.       double sleepAVG = (double) Math.round(total * 100 /daysSleepTime.size()/60) / 100.00;
  18.       System.out.println("Your sleep average is " +
  19.       Math.round(Math.floor(sleepAVG)) + " hours and " + Math.round(
  20.       ((sleepAVG - Math.floor(sleepAVG)) * 60)) + " minutes.");
  21.          }
  22.      
  23.       catch (FileNotFoundException e) {
  24.       System.out.println("An error occurred.");
  25.       e.printStackTrace();
  26.       }
  27.    }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement