Time calculator java code

Write a program that asks the user to enter a number of seconds.

There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.

There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3,600, the program should display the number of hours in that many seconds.

There are 86,400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86,400, the program should display the number of days in that many seconds.




This is my coding. When I run it and enter for example 90 seconds, the result says 0 minutes and 90 seconds. How do I change this so that it changes to 1 minute and 30 seconds? Also I need it to say how many days and hours also when I enter an amount of seconds. What am I doing wrong please help.





import java.util.Scanner; 

public class TimeCalculator 

{ 
public static void main(String[] args) 
{ 
Scanner input = new Scanner(System.in); 
int seconds, minutes, hours, days; 

seconds = 1/60; 
minutes = seconds / 60; 
hours = seconds / (60 * 60); 
days = seconds / (24 * 60 * 60); 



System.out.println ("Enter a number of seconds:"); 
seconds = input.nextInt(); 



if (seconds < 60.0) 
{ 
System.out.println ("The time entered is:" + seconds + "seconds"); 
} 

else if (seconds >= 60 && seconds <= 3599) 
{ 
System.out.println ("The time entered is:" + minutes + "minutes and" + 
seconds + "seconds."); 
} 

else if (seconds >=3600 && seconds <= 85499) 
{ 
System.out.println ("The time entered is" + hours + "hours," + minutes + 
"minutes and" + seconds + "seconds."); 
} 

else if (seconds >=86400) 
{ 
System.out.println ("The time entered is:" + days + "days," + hours + 
"hours," + minutes + "minutes and" + seconds + "seconds."); 
} 

} 

}

Open in new window

drunk0ffhateAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Do you know how to convert between minutes and seconds (not in code, but mathematically)?
0
ozoCommented:
you are setting minutes in line 12 before the user has entered the number of seconds in line 19
0
drunk0ffhateAuthor Commented:
kaufmed
um, i dont think so? im not sure sorry


ozo
so do i switch those around? i did it and im getting the results all as 0 seconds when i put in a time. how do i fix this?
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

ozoCommented:
What did you do to get  0 seconds ?
0
CEHJCommented:
Examine the resultant variable values in lines 11 to 14 and note how integer division works in Java
0
awking00Information Technology SpecialistCommented:
A couple of issues. This looks like a homework assignment so we can't offer specific solutions but can provide guidance on how to approach the problem. You can not set values to any of your variables until your input value exists in a variable. When applying your divisors you will likely end up with some decimal places which are not allowed in an int (perhaps use double). When you reach your final values for the days, hours, minutes, and seconds, you can convert them to int before passing along to your sysout.println method. Rather than start with a range of seconds you might consider calculating days first (i.e. input/86400), if this results in a fraction what's left of the decimal is the number of whole days and what's right of the decimal is the fractional portion of a day. Multiplying the fractional portion by 24 will result in the hours. Again, if that result is a fraction the what's left of that is the number of whole hours. You can repeat the process of the fractional portion of the hours times 60 to get the whole minutes and fractional minutes, then once more go through the process to get the seconds. Your sysout statements can also be modified to show blank strings for those time segment values that equal zero. Hope this helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.