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 2 of 2

Thread: A program that reads in secounds, and prints out hours minutes

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

    Default A program that reads in secounds, and prints out hours minutes

    Hi, im new to Java and just testing something out. (Sorry for my bad english)

    How do i make a java application where the user input secounds, and the program "transforms" these secounds into hours, minutes and secounds.

    Like this:

    11873

    --> 3 hours, 17 minutes, 53 secounds.

    Anyone got a suggestion with some simple code?(so i can understand it )
    I cant find a way to do it.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: A program that reads in secounds, and prints out hours minutes

    hmm.. ok. I won't give you the exact code, but i'll give you hints.

    there are 60 seconds in a minute, and 3600 seconds in an hour.

    So, 11873/3600 should give you how many hours there are. However, because we want the hours as an integer and not some decimal, we want to make sure that we either cast to int or use the int data type.
    System.out.println("5/3 = " + 5/3); // prints out 1 because 5 and 3 are recognized as int's
    System.out.println("5.0/3.0 = " 5.0/3.0); // prints out 1.666666666... because 5.0 and 3.0 are recognized as doubles

    The number of hours and seconds would be the remainder of seconds left after the hours have been taken out. An easy way to get this value is by using the modulus operator. It's basically a divide operation, but instead of giving the quotient, it gives the remainder

    System.out.println("The remainder of 100/3 is " + 100%3); // should print out 1

    The left-over seconds now must be split into minutes and seconds. I'll leave that up to you, but here's a hint: copy what i said above and make a few changes

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Json (September 15th, 2009)

Similar Threads

  1. “Getting started with RMI” tutorial for beginners
    By danielstoner in forum Java Programming Tutorials
    Replies: 0
    Last Post: October 7th, 2008, 11:18 PM