Sum mm:ss of a jtable column

Asked

Viewed 172 times

0

Good afternoon, I know that the way I’m going to explain it is difficult to help me.

I have a jTable that contains a column where times are shown, those times in mm:ss. I need to average between these times. I’ve made several attempts with different alternatives to achieve the goal but I can’t even think of one form to carry out this calculation.

My difficulty: We have the values inside jtable’s Colum, so these values are strings.

What I tried to:

  1. I tried to create a converter for mm:ss with Simpledateformat and store the information in a vector, where after that it would try to add the dates.

Upshot: a Bos**, I could not add up the way I imagined I would because as much as I was using Simpledateformat with mm:ss he returned to me Thu Jan 01 00:00:10 BRT 1970.

Only usable part of my code:

for (int i = 0; i < table.getRowCount(); i++) {
        tempo_chegada = table.getValueAt(i, 5).toString();
    }

Column da jtable

I can’t think of a logic of how I’m going to add up when I only have strings from a column of a jtable that can exist 1000 lines

I know it’s vacant because I don’t have a well-founded piece of code, but I have no idea where to start

2 answers

1

Something you can do is this:

  • Retrieve Data in String Form.

Use this code to get the number of minutes and seconds in different variables

String str = (String) q.getSingleResult();

int posicao = str.indexOf(':');
if (posicao >= 0) {
  int minutos = str.substring(0, posicao);
  int segundos = str.substring(1, posicao);
}

From there, multiply the amount of minutes by 60, and disappear with the amount of seconds to get the total of seconds for that line. Place this part of the code inside a loop so that you do this calculation for all rows in that column and at the end divide by the number of rows in the table, use the variable "i" to know how many rows were processed.

If you have any questions, comment here.

1


You got off to a good start, you just got stuck handling the data I guess..

Tentei criar um conversor para mm:ss com SimpleDateFormat e armazenar as 
informações em um vetor, onde apos isso tentaria somar as datas.

Go back to this point of the idea and turn the dates into long with Date.getTime() to realize the media.

Something like this http://ideone.com/g49YRh

Browser other questions tagged

You are not signed in. Login or sign up in order to post.