Results 1 to 3 of 3
Thread: how to sum the values
- 05-26-2010, 07:42 AM #1
Member
- Join Date
- May 2010
- Posts
- 22
- Rep Power
- 0
how to sum the values
i got the output like thisJava Code:long totalTimeInMillis=0; Set<String> dates = new HashSet<String>(); ArrayList<Integer> l=new ArrayList<Integer>(); ArrayList<Integer> m=new ArrayList<Integer>(); Map <String, Long> datesAndTotal = new HashMap<String, Long>(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Timestamp t1 = null; Timestamp t2 = null; int count = 0; long sum = 0; while ( rs.next() ) { String gpstime = rs.getString("GpsTime"); if (dates.add(gpstime)) { Date d2 = rs.getDate(1); t2 = rs.getTimestamp(1); count++; if (t1 == null ) { int nano = t2.getSeconds(); // System.out.println(nano); // System.out.println(count); } else { totalTimeInMillis = (t2.getTime() - t1.getTime()) / 1000; String dateString = df.format(d2); if (datesAndTotal.containsKey(dateString)) { m.add((totalTimeInMillis < 80) ? (int) totalTimeInMillis : (int) (totalTimeInMillis % 60)); for (int y=0; y<m.size();) { sum+=m.get(y); y++; } sum+= datesAndTotal.get(dateString)/-1; } datesAndTotal.put(dateString, sum); } t1 = t2; } } List mapKeys = new ArrayList(datesAndTotal.keySet()); List mapValues = new ArrayList(datesAndTotal.values()); Collections.sort(mapValues); Collections.sort(mapKeys); LinkedHashMap Maps = new LinkedHashMap(); Iterator valueIt = mapValues.iterator(); while (valueIt.hasNext()) { Object val = valueIt.next(); Iterator keyIt = mapKeys.iterator(); while (keyIt.hasNext()) { Object key = keyIt.next(); if (datesAndTotal.get(key).toString().equals(val.toString())) { datesAndTotal.remove(key); mapKeys.remove(key); Maps.put(key, val); break; } } } Iterator it1 = Maps.entrySet().iterator(); String prevKey=""; while (it1.hasNext()) { Map.Entry entry = (Map.Entry) it1.next(); String presentKey=(String)entry.getKey(); if(prevKey.length()==0){ System.out.println(presentKey+"->"+entry.getValue()); Long results=(Long) entry.getValue(); System.out.println(results); int hours = (int) (results / 3600), remainder = (int) (results % 3600), minutes = remainder / 60, seconds = remainder % 60; String disHour = (hours < 10 ? "0" : "") + hours, disMinu = (minutes < 10 ? "0" : "") + minutes , disSec = (seconds < 10 ? "0" : "") + seconds ; if (count < 2) { int se = Integer.parseInt(disSec); String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds(); System.out.println("00" + ":" + "00" + ":" + min + " hh:mm:ss"); } } else{ Long present=(Long)entry.getValue(); Long prev=(Long) Maps.get(prevKey); long result=present-prev; System.out.println("("+presentKey+")-("+prevKey+")->"+result); System.out.println(result); int hours = (int) (result / 3600), remainder = (int) (result % 3600), minutes = remainder / 60, seconds = remainder % 60; String disHour = (hours < 10 ? "0" : "") + hours, disMinu = (minutes < 10 ? "0" : "") + minutes , disSec = (seconds < 10 ? "0" : "") + seconds ; if (count < 2) { int se = Integer.parseInt(disSec); String min = (t2.getSeconds() < 10 ? "0" : "") + t2.getSeconds(); System.out.println("00" + ":" + "00" + ":" + min + " hh:mm:ss"); } System.out.println(disHour +":"+ disMinu+":"+disSec+ " hh:mm:ss"); } prevKey=(String)presentKey; } }catch(Throwable th) { Logger.getLogger(testing1.class.getName()).log(Level.SEVERE, null, th); }finally { if(preparedStatement!=null) preparedStatement.close(); if(conn!=null) conn.close(); } } }
2010-05-11->17030
17030
(2010-05-12)-(2010-05-11)->545
545
00:09:05 hh:mm:ss
(2010-05-13)-(2010-05-12)->13452
13452
03:44:12 hh:mm:ss
(2010-05-14)-(2010-05-13)->11572
11572
03:12:52 hh:mm:ss
(2010-05-15)-(2010-05-14)->10556
10556
02:55:56 hh:mm:ss
(2010-05-16)-(2010-05-15)->9036
9036
02:30:36 hh:mm:ss
i need to add all the values
17030+545+13452+11572+10556+9036=>62191(output)
how to change the given code to get the above output
- 05-26-2010, 07:59 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Skimming through your code I think you want to add all the values of the variable 'result'. Initialize another variable, say, 'grandTotal' to 0 before those loops start and after you have printed out 'result', add it to 'grandTotal'. When those loops have finished that variable contains the sum of it all.
kind regards,
Jos
- 05-26-2010, 10:20 AM #3
Member
- Join Date
- May 2010
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
null values
By jabo in forum New To JavaReplies: 3Last Post: 03-31-2010, 03:44 PM -
Need help with Max or min values!
By rasmus07 in forum New To JavaReplies: 13Last Post: 03-03-2010, 05:04 PM -
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks