# User input int value that translate to hours...

• 03-08-2011, 03:13 AM
yosi199
User input int value that translate to hours...
Sorry for being unclear, I will do my best to explain my thoughts. I am building an app that takes user input (as ints), and let him define a work shift, something that goes like:

please tell me when do you begin work?

_________________
(the user writes) 7

when do you finish?

(again user) 12
_________________

Now, I need some way to diffreniate between 12AM and 00PM, making sure the user will input the right values. Something that will only accept values that correspond to day/night hours. Otherwise if the user enters 7 as a starting hour and 2 as the ending hour, well... you know where this is going :)

• 03-08-2011, 03:31 AM
sunde887
You could swap to using military time. You can also consider asking them whether they start in the am or pm, and use that to determine the number. You could also determine the distance from n1 - 12, and add it to n2

so for 7-2 would be
Code:

`(12 - 7) + 2`
• 03-08-2011, 03:37 AM
yosi199
I have actually thought about the first two suggestions and thinking about the military time option. If I go for that option I might get values entered such as 15:45. How will I convert these kind of values to something I could run calcs on?

I mean how can I make 15:45 == 15 3/4?

I'm a real noob in programming, but I'm learning :)
• 03-08-2011, 03:47 AM
sunde887
If you do 45 / 60 what do you get? What do you get with 3 / 4? If you found a way to not use military time, how would you compare 2:45pm to 2 3/4?

I think the easiest would be to do 12 - n1 + n2.
Code:

```n1 = 7; n2 = 5; n3 = 12 - n1 + n2; System.out.println(n3);```
That will produce the number of hours you want, 10. This is a pretty easy conversion as well.

I honestly feel this would be the best implementation for what you want.
• 03-08-2011, 03:49 AM
Junky
If you get 15:45 as input then it has to be a String. Then you can use the split method to get the hours and minutes.
• 03-08-2011, 03:59 AM
yosi199
Quote:

Originally Posted by sunde887
If you do 45 / 60 what do you get? What do you get with 3 / 4? If you found a way to not use military time, how would you compare 2:45pm to 2 3/4?

I think the easiest would be to do 12 - n1 + n2.
Code:

```n1 = 7; n2 = 5; n3 = 12 - n1 + n2; System.out.println(n3);```
That will produce the number of hours you want, 10. This is a pretty easy conversion as well.

I honestly feel this would be the best implementation for what you want.

OK, thats seems to work for finding the amount of hours, so now just let me add a new factor - I also want the user to enter an hour from which he starts to earn 150% compared to a regular hour.

I'm guessing I will now add to the code "n4" that represnt the 150% starting hour, something like this:

n1 = 7;
n2 = 5;
n2.5 = 3; //User starts to earn 150% compared to other hours
n3 = 12 - n1 + n2;
n4 = 12 - n2.5;
System.out.println(n3);

is this right?
• 03-08-2011, 04:00 AM
yosi199
Quote:

Originally Posted by Junky
If you get 15:45 as input then it has to be a String. Then you can use the split method to get the hours and minutes.

• 03-08-2011, 04:06 AM
sunde887
not entirely.

Lets say they begin work at 7 and they work till 7 and you want to start 150% pay at 3. will then solve this by finding how many hours they worked until 3. How would you do that? Do that for me and give an answer and we can move onto the next step.

http://www.java-forums.org/java-tips...-function.html
• 03-08-2011, 04:14 AM
yosi199
Quote:

Originally Posted by sunde887
not entirely.

Lets say they begin work at 7 and they work till 7 and you want to start 150% pay at 3. will then solve this by finding how many hours they worked until 3. How would you do that? Do that for me and give an answer and we can move onto the next step.

http://www.java-forums.org/java-tips...-function.html

I could build a function that compares the start of shift to end of shift and if they are equal, assume shift time == 12 hours and then do the 150% calc based on 12 hours?
• 03-08-2011, 04:22 AM
Junky
Quote:

Originally Posted by yosi199

It is a method of the String class. Read the Java API as it has all the info you need.
• 03-08-2011, 04:32 AM
sunde887
Thats not what I was looking for. If someone works 7 - 7, you first want to find how many hours from 7 - time and a half time, in my example, till 3. Using the method I showed you, how many hours would 7 - 3 be?
• 03-08-2011, 04:37 AM
yosi199
Quote:

Originally Posted by sunde887
Thats not what I was looking for. If someone works 7 - 7, you first want to find how many hours from 7 - time and a half time, in my example, till 3. Using the method I showed you, how many hours would 7 - 3 be?

I'm sorry I didn't understan what do mean by this:

"from 7 - time and a half time"

Please be patient with me I've been studying all night its now 05:49am here :)
• 03-08-2011, 04:43 AM
sunde887
I am glad to be patient. Not much else going on tonight and I want to help out.

What time do you want to start 150%? Lets say its 3. How would you determine the hours from 7 - 3?
• 03-08-2011, 04:57 AM
yosi199
Assuming the shift is from 7 till 7, I was thinking about subtracting 3 from 7 (the end of shift or say, endOfShift - shift150). This should get me 4 hours of 150% pay.

Now, if I already found out that by entering startOfShift = 7 and endOfShift = 7, the user meant a 12 hours shift, then I would only need to: totalHoursOfShift - 4, to find the remaining regular 100% pay hours.

At the end I will want to find based on a few inputs from user like:

payHourly // user enters how much he earns for each regular hour
startOfShift
endOfShift
shift150

Based on these parameters it will calculate how much money the user have earned in this shift including both 100% and 150% hours pay.
• 03-08-2011, 05:04 AM
sunde887
You are correct. I was thinking of solving how many hours from start until the hour where they get time and a half.

If they begin making time and a half at 3, then 7 - 3 = 12 - 7 + 3 = 8 + 7 - 3(end - time and a half time) = 4 = 12.

With this method you can find out that 8 hours will earn normal pay and 4 will earn time and a half. Then you can do

payrate * 8 +((payrate * 4) * 1.5)
• 03-08-2011, 05:18 AM
yosi199
Quote:

Originally Posted by sunde887
You are correct. I was thinking of solving how many hours from start until the hour where they get time and a half.

If they begin making time and a half at 3, then 7 - 3 = 12 - 7 + 3 = 8 + 7 - 3(end - time and a half time) = 4 = 12.

With this method you can find out that 8 hours will earn normal pay and 4 will earn time and a half. Then you can do

payrate * 8 +((payrate * 4) * 1.5)

I already manged to do all the calcs and get the info I want. My problem is that I have to make sure somehow that I wont have to deal with one user inputting:

startOfShift = 7
endOfShift = 7
shift150 = 3

and then another user will come and write it in a totally different way:

startOfShift = 19:00
endOfShift = 7
shift150 = 3

My original questions was whats the best way to deal with these? Should I lock the user to one explictly defined way of inputting? or maybe letting them have a free hand and I will try compensate by covering a few ways the user can enter the shift details?

I think I will have to learn the split method cause at the end I will to be quite accurate with the user's input so they could write accurate shifts like:

start at 2:34
end at 00:14

and such...
• 03-08-2011, 05:30 AM
sunde887
Since it's your program you can decide this, however; it is probably better to have more flexible code which makes the user have a better experience.

A new idea I had was for you to create a time class. You can create a constructor which can set up the correct time based on military or regular time, and also convert between military and regular.

something like
Code:

```class Time{   int hours;   int minutes;   boolean am;   //constructor for military   Time();   //constructor for regular   Time();   //convert to military   public Time toMilitary()   //convert to regular   public Time toRegular()   //difference   public Time difference(Time t)   public String toString() }```
you can also add more methods of your own. This way you can create a time object and make sure they are both using the same format then calculate the difference.