Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Time (min:sec) conversion within a list object (http://www.velocityreviews.com/forums/t126559-time-min-sec-conversion-within-a-list-object.html)

The DUDE 09-10-2003 11:28 PM

Time (min:sec) conversion within a list object
 
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?

VisionSet 09-11-2003 01:09 AM

Re: Time (min:sec) conversion within a list object
 
"The DUDE" <mountd@yahoo.com> wrote in message
news:21e8983e.0309101528.66fc1689@posting.google.c om...
> 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



Kabal 09-11-2003 10:57 AM

Re: Time (min:sec) conversion within a list object
 
mountd@yahoo.com (The DUDE) wrote in message news:<21e8983e.0309101528.66fc1689@posting.google. com>...
> 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;
}
}

code learner 12-24-2010 03:50 PM

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);
}
}


All times are GMT. The time now is 06:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.