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

Thread: Time String to int seconds

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Time String to int seconds

    Please help!

    I am trying to convert a time string to an integer in seconds (eg. 1:25 = 85 secs) but cannot seem to figure out how best to go about doing this. I have so far used a delimiter to remove the ":" from the string and separate the minutes from the seconds. I also have a method to take minutes and seconds and add them to form only seconds. Both methods are separate but I would like to combine them somehow. Could someone please show me how this is possible. I may not even be on the right track and it is proving very frustrating. Thanks

    public void trackLength() {
    double seconds = 0, secs, mins;
    Scanner Input = new Scanner(System.in);

    // String to split.
    String str = "3:25";
    String[] tempString;

    // delimiter
    String delimiter = ":";

    // given string will be split.
    tempString = str.split(delimiter);

    System.out.println("Enter minutes: ");
    mins = Input.nextDouble();

    System.out.println("Enter seconds: ");
    secs = Input.nextDouble();

    //makes calculations of the values needed
    seconds += secs;
    seconds += 60 * mins;
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Time String to int seconds

    I'm really not sure what this method is supposed to be doing. Why are you getting user input in the middle of converting hard-coded input?

    You're were closer when you had things split up into separate methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time String to int seconds

    What I'm trying to achieve is one method not two. I simply need it to convert the time string into int seconds ready for later manipulation. I am guessing I am going about this the wrong way but I cannot think of a better way to do it. sorry

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Time String to int seconds

    If you want a single method, can't you just call the other two methods from that single method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time String to int seconds

    Call me dumb lol. But I really wouldn't know how. I am just a beginner at this and am really drowning at the moment but am determined to grasp it somehow. Is there a working example anywhere that I can access to see how it works?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Time String to int seconds

    I don't know, say you have these two methods:

    public void doThing1(){
       //thing 1
    }
     
    public void doThing2(){
       //thing 2
    }

    ...and then you are tasked with creating a single method that accomplishes both thing 1 and thing 2. Instead of copying and pasting both things into a new method, doesn't it make sense just to do this?

    public void doBothThings(){
       doThing1();
       doThing2();
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time String to int seconds

    I understand what you mean but what I want to do is remove this manual input:

    System.out.println("Enter minutes: ");
    mins = Input.nextDouble();

    System.out.println("Enter seconds: ");
    secs = Input.nextDouble();

    and create an automatic input from the minutes and seconds generated from this method

    double seconds = 0, secs, mins;
    Scanner Input = new Scanner(System.in);

    // String to split.
    String str = "3:25";
    String[] tempString;

    // delimiter
    String delimiter = ":";

    // given string will be split.
    tempString = str.split(delimiter);

    and then convert them into ints to be converted into seconds with this part of the method

    //makes calculations of the values needed
    seconds += secs;
    seconds += 60 * mins;

    Does that make things a bit clearer. I know what I am trying to achieve but its hard to explain. sorry again but thanks for taking the time to help.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 Posts

    Default Re: Time String to int seconds

    I think your requirements are only a little bit more complicated than my example.

    My doThing1() method, for you, should take no parameters, but prompt a user for input and return a String such as "3:25".

    My doThing2() method, for you, should take a single String parameter, such as "3:25", convert it into minutes, and return that as an int.

    My doBothThings() method, for you, should call the doThing1() method to retrieve the String, then pass it into the doThing2() method to get the int, then do something with that int.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Time String to int seconds

    Ok I think I understand what your saying. I am clear on your theory. My time values are minutes and seconds extracted from strings within a file. What I am having difficulty in doing is the conversion from 3:25 as a string to 205 seconds as an integer. My simple question would be then how is this achieved. Do I still use two methods or can this be achieved in one. So String to int conversion (in seconds).

  10. #10
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Time String to int seconds

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/56393-time-string-seconds-int.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


Similar Threads

  1. Converting decimal number to m minutes, n.nn seconds format.
    By coreysizemore in forum Java Theory & Questions
    Replies: 1
    Last Post: January 20th, 2012, 10:37 PM
  2. Setting Boolean to 'True' after X amount of Seconds?
    By uhKenKaniff in forum Java Theory & Questions
    Replies: 1
    Last Post: December 12th, 2011, 06:29 PM
  3. Go through string one char at a time
    By joshft91 in forum Java Theory & Questions
    Replies: 7
    Last Post: April 18th, 2011, 12:19 AM
  4. I want to extract minutes and seconds from duration
    By msa969 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 02:14 PM
  5. Really Quick n00b question, should take three seconds to answer
    By joeschmidt45 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 24th, 2011, 04:23 AM

Tags for this Thread