Operations on TableRow object? Can't find sum

/*data.csv looks like this- 
                            header
                            1,
                            1,
                            1,
  */                          
Table table;
table = loadTable("data.csv","header");

for(int i = 0; i < table.getRowCount();i++){
   TableRow row = table.getRow(i);
   int a = row.getInt(0);

    println(a);
  }

I know that this is a very basic misunderstanding of the data type, but I’ve spent way too long on this problem and I don’t have anyone available to ask. How would you find the sum of (a), all rows in column (0)?

1 Like

Declare b before the for loop and say
b = b+a; in the loop

1 Like

Hello @syzygy,

Here is an example with products:

It will have to be adapted for sum.

Make use of println() statements to help understand what your code is doing.

:)

Thanks, that helps. println(b) returns
1
2
3

I understand that the operation goes something like this:
1 = (0+1)
2 = (1+1)
3 = (2+1)

But what I really need is for the sum to be an int with a value of 3. I feel like the Table class can be really powerful, but there’s something that I’m not conceptually grasping about the data type

1 Like

println gives the correct result only after the for loop

During the for loop it’s going over the rows and adds
data

2 Likes

Not sure what you are getting at

Try with other data in your csv

1 Like

dumb mistake. thanks

1 Like