Results 1 to 7 of 7
  1. #1
    Scruss is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Turn seconds to hours/minutes/sec

    Hello guys,

    I recently started with Java programming, and I'm trying to make a program which turns seconds to x hours, y minutes and z seconds. Also if my input is 3755 seconds, my printout should be 1 hour, 2 minutes and 35 seconds.

    This is what I thought so far which seems to work pretty well:
    Java Code:
                    String s = JOptionPane.showInputDialog(null, "Insert seconds you want to convert: ")
                    int seconds;
    		seconds = Integer.parseInt(s);
    
    		int hour = seconds/3600;
    		int restHour = seconds%3600;
    		int minutes = restHour/60;
    		int sec = restHour%60;
    So to my question. I want to seperate my printout between singulars and majorities, also hour/hours, second/seconds and minute/minutes. If I get the value 0, I don't want that on my printout, for example if I type 60 seconds, I want 1 minute as printout and not 1 minute and 0 seconds.

    Could anyone help me with this program? What should I do? I tried the if-else statement, but it just turns into a mess.. Any help? :/

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Turn seconds to hours/minutes/sec

    Quote Originally Posted by Scruss View Post
    ...I tried the if-else statement, but it just turns into a mess.. Any help? :/
    The solution is of course to use if/else blocks. You really need to show us your "mess"/attempt. Else how will we know what you're doing wrong?

  3. #3
    Scruss is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Turn seconds to hours/minutes/sec

    Quote Originally Posted by Fubarable View Post
    The solution is of course to use if/else blocks. You really need to show us your "mess"/attempt. Else how will we know what you're doing wrong?
    Here is my code:
    Java Code:
    		if(seconds == 0)
    		JOptionPane.showMessageDialog(null, "You must enter a bigger number than 0!");
    		else if(seconds == 1 || sec == 1)
    		JOptionPane.showMessageDialog(null, seconds + " second");
    		else if(seconds < 60)
    		JOptionPane.showMessageDialog(null, seconds + " seconds");
    		else if(minutes == 1)
    		JOptionPane.showMessageDialog(null, minutes + " minute");
    		else if(minutes < 60)
    		JOptionPane.showMessageDialog(null, minutes + " minutes, " + sec + " seconds");
    		else if(hour == 1)
    		JOptionPane.showMessageDialog(null, hour + " hour");
    		else if(seconds < 7200)
    		JOptionPane.showMessageDialog(null, hour + " hour" + minutes + " minutes, " + sec " seconds");
    		else if(seconds > 7200)
    		JOptionPane.showMessageDialog(null, hour + " hours" + minutes + " minutes, " + sec " seconds");
    When I watch this, I get so confused. I know I did a lot of things wrong, but I can't see them.. I can't do it right, and get the right solution :/

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,765
    Blog Entries
    7
    Rep Power
    21

    Default Re: Turn seconds to hours/minutes/sec

    I'd create a little method for the purpose:

    Java Code:
    String asString(int x, String singular, String plural) { ... }
    ... and call it like this: asString(hours, "hour", "hours")+" "+asString(minutes, "minute", "minutes")+" "+asString(seconds, "second", "seconds");

    I leave the implementation of that little method up to you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Turn seconds to hours/minutes/sec

    I would create a String and concatenate your answer to it as you walk through your variables. Something like:

    Java Code:
    String answer = "";
    
    if (hour ....) {
      answer += .... // you've got to fill in the .... of course 
    } else if (hour ....) {
      answer += ....
    }  // more?
    
    if (minutes ....) {
      answer += ....
    }  
    
    //... etc...
    And then display the String when all done rather than in a mess of JOptionPanes. Also, I don't see how your sec will ever be greater than 60 based on your math, same for your minutes.

    Note: some may tell you to use a StringBuilder here rather than a String, but for your simple app, that's a bit of overkill (in my opinion).

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

    Default Re: Turn seconds to hours/minutes/sec

    Java Code:
    System.out.printf("%d hour%s", hour, hour == 1 ? "" : "s");

  7. #7
    Scruss is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Turn seconds to hours/minutes/sec

    Thanks guys! I managed to solve my problem!

Similar Threads

  1. So I'm supposed to convert seconds to hours...
    By nope in forum New To Java
    Replies: 5
    Last Post: 02-14-2011, 12:46 AM
  2. Replies: 4
    Last Post: 09-09-2010, 12:15 PM
  3. Replies: 3
    Last Post: 04-05-2010, 02:08 PM
  4. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 08:35 AM
  5. How to beep every 5 minutes in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:40 PM

Posting Permissions

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