Time (min:sec) conversion within a list object

Discussion in 'Java' started by The DUDE, Sep 11, 2003.

  1. The DUDE

    The DUDE Guest

    I am a little stumped. I have a list (ArrayList) of strings each one
    represents a time (min:sec format). I need to find out the total
    number of minutes. Some of my quesdo code:

    // Is an ArrayList is Strings (min:sec)
    java.util.List = someObject.getTimes();

    double minutes;
    double seconds;

    while (!list.empty) {
    //get the total minutes and seconds then convert to minutes
    }

    //Help?
     
    The DUDE, Sep 11, 2003
    #1
    1. Advertising

  2. The DUDE

    VisionSet Guest

    "The DUDE" <> wrote in message
    news:...
    > I am a little stumped. I have a list (ArrayList) of strings each one
    > represents a time (min:sec format). I need to find out the total
    > number of minutes.


    > java.util.List list = someObject.getTimes(); // using this as the List


    Iterator iterator = list.iterator(); // gets a java.util.Iterator for
    examining the list

    int totalSecs = 0; // storage for our parsed data

    while (iterator.hasNext()) { // loops the List
    String string = (String)iterator.next(); // retrieves and casts a String
    object from the List
    String minStr = string.substring(0,string.indexOf(":")); // gets the
    substring before the ':'
    String secStr = string.substring(1+string.indexOf(":")); // gets the
    substring after the ':'
    int mins = Integer.parseInt(minStr); // parses the ints
    int secs = Integer.parseInt(secStr);
    totalSecs += (mins*60)+secs; // converts the mins to seconds and adds it
    to the running total
    }

    System.out.println("Total Mins = "+(float)totalSecs/60); // convert to
    minutes

    probably some typos etc, I haven't compiled it.

    If I've done your homework then it won't help you in the long run, but heh
    that's up to you.

    --
    Mike W
     
    VisionSet, Sep 11, 2003
    #2
    1. Advertising

  3. The DUDE

    Kabal Guest

    (The DUDE) wrote in message news:<>...
    > I am a little stumped. I have a list (ArrayList) of strings each one
    > represents a time (min:sec format). I need to find out the total
    > number of minutes. Some of my quesdo code:
    >


    // Here you go

    import java.text.DecimalFormat;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.StringTokenizer;

    public class Time {
    public static void main(String[] args) {
    StringTokenizer st = null;
    DecimalFormat decimalFormat =
    new DecimalFormat(".00");
    List list = fillList(false);

    double mins,secs;
    mins=secs=0.0;

    for (int i=0; i<list.size(); i++) {
    st = new StringTokenizer(
    String.valueOf(list.get(i)), ":");

    mins += Double.parseDouble(st.nextToken());
    secs += Double.parseDouble(st.nextToken());
    }

    System.out.println("Total time (in minutes): " +
    decimalFormat.format((mins + (secs / 60))));
    }

    private static List fillList(boolean showTimes) {
    List list = new ArrayList();
    Random r = new Random();
    DecimalFormat decimalFormat =
    new DecimalFormat("00");

    for (int i=0; i<10; i++) {
    list.add(
    decimalFormat.format(
    Long.parseLong(String.valueOf(r.nextInt(60))))
    + ":" +
    decimalFormat.format(
    Long.parseLong(String.valueOf(r.nextInt(60))))
    );

    if (showTimes) {
    System.out.println(list.get(i));
    }
    }

    if (showTimes) {
    System.out.println("=====");
    System.out.println();
    }

    return list;
    }
    }
     
    Kabal, Sep 11, 2003
    #3
  4. The DUDE

    code learner

    Joined:
    Dec 24, 2010
    Messages:
    19
    import java.util.ArrayList;
    public class MinuteSecond {
    public static void main(String[] args) {
    ArrayList list = new ArrayList();
    list.add("05:39");
    list.add("10:10");
    list.add("56:01");
    list.add("27:19");

    int tm = 0;
    int ts = 0;
    for(int i=0 ; i<list.size() ; i++){
    String t = (String)list.get(i);
    int m = Integer.parseInt(t.substring(0,2));
    int s = Integer.parseInt(t.substring(3));
    tm += m;
    ts += s;
    }
    tm += ts/60;
    ts %= 60;
    System.out.println("Total Time "+tm+":"+ts);
    }
    }
     
    code learner, Dec 24, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robocop
    Replies:
    3
    Views:
    7,127
    Sean Berry
    Feb 26, 2004
  2. Lois
    Replies:
    1
    Views:
    3,409
    Ryan Stewart
    Dec 27, 2004
  3. Summu82
    Replies:
    5
    Views:
    900
    Richard Heathfield
    Jun 7, 2006
  4. Richard Heesbeen

    Webservice handling amount of requests per sec/min.......

    Richard Heesbeen, May 28, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    186
    Bruce Johnson [C# MVP]
    May 28, 2005
  5. x1
    Replies:
    4
    Views:
    150
Loading...

Share This Page