Having extracted the hours, how about multiplying by 60 to get the minutes. If you then convert the two times into minutes, subtract them and divide by 60, you should get hours and the remainder in minutes.

So, 2301 and 1615 will convert to 1381 minutes and 975 minutes. Substraction results in 406, which is 6hr 46min when divided by 60.

I haven't coded this, so I'm not sure if you will have a problem in correctly getting the remainder.

post your code, and we'll be happy to look at it. it's hard to tell if you logic is wrong without seeing it.

also, tell us exactly what result you are getting vs. what result you are expecting.

finally, there may be a class that could help you, but what is the purpose of this assignment... are you supposed to find a utility class that does what you need, or are you supposed to figure out how to code it yourself?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

As a hint, snoop the JavaDoc for interesting ways to handle dates. They didn't separate time so you won't find much about time without date.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84

posted

0

Hi again here's my code

i put in 200 and 200, i'm supposed to get 0 hours and 0 mins, but i get 1.0 hours and some minutes.

I got fairly good results. One thing to note is you should not give inputs in three digits as 200 or 210 as time is always in four digits.

I put some output statements for tracking some outputs. Here I have one question: When I give input as 2100 and 0100 I got output of Math.min(2100, 0100) as 64.0? I don't why? If anybody can then please explain.

I put some output statements for tracking some outputs. Here I have one question: When I give input as 2100 and 0100 I got output of Math.min(2100, 0100) as 64.0? I don't why? If anybody can then please explain.

How do you enter these time values in your program? The behavior described leads me to believe that they are "hardcoded" in your program. If you have a number in a Java program that starts with a leading zero, it is interpreted as octal (base 8). 0100 base 8 is the same as 64 base 10. If you are unfamiliar with number systems, you should google for information on how to convert between numbers in different bases.

If you let the user enter these numbers, either as command line arguments or using an InputStream or Reader of some sort, you can avoid these issues.

you're sort of losing the original value you got from the user. i'd personally use another temp. variable, to store the hours calculated value. after all, what happens if in 3 months, you need that input value again?

doing that might actually simplify your code. to get the hours, you can just do a division by 100 and a cast. you can then use the modulus operator to get the minutes, and then get the total minutes by a simple calculation: