The purpose of the program is to take 4 user inputed integers (days, hours, minutes, and seconds) and output them as a floating point doubles to show the maximum amount of time possible for each. For example I input 2 days, 10 hours, 45 minutes, 20 seconds and it outputs 2.448148148148 days, 58.7775 hours, 3525.33333 mins, 211520 seconds. I got the program almost working but I do not know how to get the days, hours, and minutes to show the remainder.

import java.util.Scanner;

public class TimeTotalsCalc 
{

    public static void main(String[] args)
    {

        int days, hours, mins, seconds, time = 60, hours_In_Day = 24, daysOut, hoursOut, minsOut, secsOut;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Give me a value in days, minutes, hours and seconds");
        System.out.println("and I will show the total time possible for each.");
        System.out.println("");

        System.out.print("Please enter the number of days: ");
        days = keyboard.nextInt();
        System.out.print("Please enter the number of hours: ");
        hours = keyboard.nextInt();
        System.out.print("Please enter the number of minutes: ");
        mins = keyboard.nextInt();
        System.out.print("Please enter the number of seconds: ");       
        seconds = keyboard.nextInt();

        daysOut = (days);
        hoursOut = ((days * hours_In_Day) + hours);
        minsOut = ((days * hours_In_Day * time) + (hours * time) + (mins));
        secsOut = ((days * hours_In_Day * time * time) + (hours * time * time) + (mins * time) + seconds);

        System.out.println();
        System.out.println("We can represent the time you entered as any of the following: ");
        System.out.println((double)daysOut + " days(s)");
        System.out.println((double)hoursOut +  " hour(s)");
        System.out.println((double)minsOut + " minute(s)");
        System.out.println(secsOut + " seconds(s)");

        keyboard.close();


    }
}   

Recommended Answers

All 7 Replies

show the maximum amount of time possible for each.

Can you explain what that means?

how to get the days, hours, and minutes to show the remainder.

Can you explain what the "remainder" is?
Is it like this: 10/3 = 3 with a remainder of 1

I want to take the amount of time entered and add it up to output it as each variable of time. So if I entered 2 day, 10 days, 45 minutes, and 20 seconds, and wanted the total amount of that time in nothing but seconds it would output 211520 seconds.

For example correct output would look like:

Please enter the number of days: 2
Please enter the number of hours: 10
Please enter the number of minutes: 45
Please enter the number of seconds: 20

We can represent the time you entered as any of the following:
2.448148148148148 day(s)
58.75555555555556 hour(s)
3525.3333333333335 minute(s)
211520 second(s)

But I keep getting this, not showing the remaining time left over for each

Please enter the number of days: 2
Please enter the number of hours: 10
Please enter the number of minutes: 45
Please enter the number of seconds: 20

We can represent the time you entered as any of the following:
2.0 day(s)
58.0 hour(s)
3525.0 minute(s)
211520 second(s)

Have you made a list with two columns to compare? One column with the correct results and the other column with what the program outputs. Are the results on any of the rows correct?
Which ones are wrong?

Casting an int to double will not put any values to right of the decimal point.

Can you do the calculation by hand on a piece of paper? What are the expressions that you need to use to compute a fractional day given some seconds? What part of a day is 23456 seconds?

Initialize variables to zero. Not sure why days are intially = 60 ??

May I suggest converting everything to seconds.

Then try something like this (using % to get the remainder):

hours = totalSecs / 3600;
minutes = (totalSecs % 3600) / 60;
seconds = totalSecs % 60;

or something more clever like:

 public String Convert(int time) { 
   return time/24/60 + ":" + time/60%24 + ':' + time%60;
 }

//also entering time in seconds

@Starstreak If you are going to do the OPs work for him, you should also explain how the code you post works.

Explain '/' and '%' ?

Well he knows '/' (this is part of the basics) and I've explained '%', so what's left?

What does this do?

  return time/24/60 + ":" + time/60%24 + ':' + time%60;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.