Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

    Default how to sum the values

    Java 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();
    
          }
       
      
    }
    }
    i got the output like this
    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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  3. #3
    Join Date
    May 2010
    Posts
    22
    Rep Power
    0

Similar Threads

  1. null values
    By jabo in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 03:44 PM
  2. Need help with Max or min values!
    By rasmus07 in forum New To Java
    Replies: 13
    Last Post: 03-03-2010, 05:04 PM
  3. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-10-2009, 11:35 PM
  4. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •