Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Time (min:sec) conversion within a list object

Reply
Thread Tools

Time (min:sec) conversion within a list object

 
 
The DUDE
Guest
Posts: n/a
 
      09-10-2003
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?
 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      09-11-2003
"The DUDE" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Kabal
Guest
Posts: n/a
 
      09-11-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (The DUDE) wrote in message news:<(E-Mail Removed). 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;
}
}
 
Reply With Quote
 
code learner code learner is offline
Junior Member
Join Date: Dec 2010
Posts: 19
 
      12-24-2010
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);
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of Python object to perl object srinivasan srinivas Python 0 08-17-2009 10:47 AM
Time conversion between UTC and local time andreas.profous@googlemail.com Python 2 02-09-2008 02:54 AM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Using a Page object within a Page object Kevin Phifer ASP .Net 4 01-06-2004 06:38 AM



Advertisments