Results 1 to 17 of 17
  1. #1
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default 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 :)

    Can someone please give me an hint on which way to go about this?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java Code:
    (12 - 7) + 2

  3. #3
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    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 :)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.
    Java 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.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    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.

  6. #6
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.
    Java 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?

  7. #7
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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.
    Can you give me more info about this split method? even direct me to a url or something, I would like to learn this method too...

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Last edited by sunde887; 03-08-2011 at 03:08 AM.

  9. #9
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default

    Quote Originally Posted by yosi199 View Post
    Can you give me more info about this split method?
    It is a method of the String class. Read the Java API as it has all the info you need.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  12. #12
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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 :)

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  14. #14
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    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.

    Maybe a little more info will help us...
    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.
    Last edited by yosi199; 03-08-2011 at 04:04 AM.

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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)

  16. #16
    yosi199 is offline Member
    Join Date
    Mar 2011
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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...

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    Java 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.

Similar Threads

  1. Help with user input
    By sconniegorilla in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 02:00 PM
  2. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 03:09 AM
  3. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-15-2010, 11:02 PM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 04:47 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •