Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Formulas within my code aren't cooperating

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Formulas within my code aren't cooperating

    I'm a novice programmer and I am trying to solve this annoying problem with my code.

    import java.util.Scanner;

    public class TestLab100
    {
    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);
    int milisec, sec, hour, minute, remainderMi, remainderS, remainderH, remainderM;

    System.out.print("Starting milli-seconds: ");
    milisec = keyboard.nextInt();
    hour = milisec/ 3600000;
    remainderH = milisec% 3600000;
    minute = remainderH/ 60;
    remainderS= minute/1000;
    remainderM = minute%1000;
    remainderMi = remainderM% 1000;
    System.out.println("Hours: " + hour);
    System.out.println("Minutes: " + remainderS);
    System.out.println("Seconds: " + remainderM);
    System.out.println("Milli Seconds: " + remainderMi);

    }
    }

    I need the seconds to result in 40 and the milliseconds to be 123; well that's what they should be. Thanks for any help.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Formulas within my code aren't cooperating

    And what do you get printed instead?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    --------------------Configuration: <Default>--------------------
    Starting milli-seconds: 10000123
    Hours: 2
    Minutes: 46
    Seconds: 668
    Milli Seconds: 668

    Process completed.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    Also you need to show your input.
    For easier testing, change the input statement by hardcoding a value. For example:
    milisec = 12345; //keyboard.nextInt();

    When you get your formulas working then you can restore it to get the user's input.

  5. #5
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    import java.util.Scanner;

    public class TestLab100
    {
    public static void main(String[] args)
    {
    Scanner keyboard = new Scanner(System.in);
    int milisec, sec, hour, minute, remainderMi, remainderS, remainderH, remainderM;

    System.out.println("Starting milli-seconds: ");
    milisec = 12345; //keyboard.nextInt();
    hour = milisec/ 3600000;
    remainderH = milisec% 3600000;
    minute = remainderH/ 60;
    remainderS= minute/1000;
    remainderM = minute%1000;
    remainderMi = remainderM% 1000;
    System.out.println("Hours: " + hour);
    System.out.println("Minutes: " + remainderS);
    System.out.println("Seconds: " + remainderM);
    System.out.println("Milli Seconds: " + remainderMi);
    }
    }

    this is the output:

    --------------------Configuration: <Default>--------------------
    Starting milli-seconds:
    Hours: 0
    Minutes: 0
    Seconds: 205
    Milli Seconds: 205

    Process completed.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    Are the answers correct? Have you worked it out by hand to know what the correct answers are?
    205 seconds looks like too many.

    You need to take a piece of paper and pencil and do all the computations manually to see what the arithmetic is for computing each value.
    Also to verify that your equations are doing what you expect, you should print out the results of ALL the steps. Looking at that printed output will tell you where your problem is.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    well the original project was for my class and was to make 10000123 milliseconds into hours minutes and so on. I wanted to make it able to have user input though. For that (^) number of milliseconds it should be 2 hours, 46 minutes, 40 seconds, and 123 milliseconds. I am just confused on how the formulas should be. Again I am a novice programmer.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    I am just confused on how the formulas should be
    This problem is pure arithmetic, very little programming required.
    We'll start with two formulas:
    How do you get the total number of seconds from millisecs?
    How do you get the number of hours from a total number of seconds?
    Last edited by Norm; September 2nd, 2011 at 02:54 PM.

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    seconds = milliseconds/1000
    hours = seconds/3600

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    How would you convert 7925 seconds to hours, minutes and seconds?
    Can you do that on a piece of paper?

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    2 hours, 12 minutes, 5 seconds?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    That looks right.
    how did you do it? What were the steps you took to compute those values?
    Write down the steps you used one by one.
    That will be a guide for how to write the formulas for your program.

  13. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    the seconds divided by 3600, the remaining number divided by 60, then the rest was the 5 seconds.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    Your steps are not quite complete.
    What is the "remaining number"? and What is the "rest"?
    You need formulas to compute those values.

  15. #15
    Junior Member
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formulas within my code aren't cooperating

    ill do the exact ones for this problem,

    7925 divided by 3600 resulting in 2, then 725 divided by 60 resulting in 12, and then the remaining 5 seconds. Sorry if I'm not really good with this ha

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Formulas within my code aren't cooperating

    Where did the 725 come from?
    Where did the 5 come from?
    What are the equations to compute those numbers?

Tags for this Thread