Time (min:sec) conversion within a list object

T

The DUDE

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?
 
V

VisionSet

The DUDE said:
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.
 
K

Kabal

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;
}
}
 
Joined
Dec 24, 2010
Messages
19
Reaction score
0
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);
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top