Search code examples
javaloopsswingfor-loopjtable

How can I add content of different rows of my jTable in a variable?


sorry if my question looks stupid but I'm not really good at For loops. My window looks like this.

jFrame

When I click on "valider", I want to add every rows in the Prix column to a variable Total. Here is my loop:

float total = 0;
 for (int i = 0; i < jTable4.getRowCount(); i++)
 total =+ (float) jTable4.getValueAt( i, 2);

When I check what's in my Total variable, it just gives me the content of the last row.

Could you guys help me with this loop ?


Solution

  • Problem is you have used an invalid assignment Operator . =+ should be change as += to get your expected answer .

    
    float total = 0;
    for (int i = 0; i < jTable4.getRowCount(); i++) {  // Loop through the rows
           
            total += (float) jTable4.getValueAt(i, 2);  
    }