• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sum up the last column of JTable  RSS feed

 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have multiple JSpinner. When it is clicked, the name,quantity and price will be added into JTable. I also have one JTextField, used to sum up the total price, which is in the last column of the table.

To get the total of the last column of each row, I use this code

Example (Correct)

When I change the qty from 2 to 1, it sum up instead of minus
 
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the variable total declared?
It looks like it's not local to the method, so the value is retained between calls to stateChanged.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Where is the variable total declared?
It looks like it's not local to the method, so the value is retained between calls to stateChanged.

I declared it as global
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you initialize total to zero?
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Where do you initialize total to zero?

I declared it as global.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to solve ?
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, think it through.
At the moment you have a "global" total value and, when the user changes a number in the table, you then add the new value for that row to this total.

So in your example, your total value was 17, the user changes the value in one row so it now says 6.  Your code adds that 6 to the total...so it's now 23.

So what should be done to ensure the total is calculated correctly?
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't keep a "total" variable.

Instead, whenever any of the values in your table change that will affect the total, simply iterate through all the rows in the table to recalculate the total.

This way you don't need to how the values change. That is if the quantitly goes from 2 to 3, or 2, to 1, the logic is still the same, you don't need to worry about calculating the difference from the previous value.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Don't keep a "total" variable.
simply iterate through all the rows in the table to recalculate the total.
Can you explain more on this ?
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you already have looping code, but you only ever process one row in the loop.

I am suggesting you get rid of the if condition and recalculate the total as you loop through each row. So before the loop starts you set the total value to 0, then for each row you calculate the row amount and add it to the total. When the loop finishes you then display the total value.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please correct me if I am doing wrong. I getting java.lang.NumberFormatException error

Error


 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't debug a NumberFormatException.

Only you have the data. Your data is not a number.

Add some System.out.println(...) statements to see what the data is that you are actually trying to convert to a number. Or use a debugger to step through the code to see what your data actually is.

This is basic debugging.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Qty is the title, which is in the first row of the JTable.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a title string in the first row of the table? Why not just use column headers, which aren't part of the table?

But anyway if you're going to have the first row of the table be text strings describing the rest of the rows, then clearly you wouldn't bother trying to include the first row when summing up the rest of the rows, which is where the data actually is.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I get rid of the first row of JTable in order to get the total value ?
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the latest code I tried

By using above code, I get output qty and price. If I change row to rows in this two lines,
I get error

 
Norm Radder
Rancher
Posts: 2240
28
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't belive I've been stucked at this part almost one week !
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 3 you set "rows" to the row count. One line 5 you loop while row < rows. On line 8 you decrement the number of rows but that is not reflected in your "rows" variable.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Joe wrote:How should I get rid of the first row of JTable in order to get the total value ?


That's entirely the wrong way to look at it. Don't get rid of the first row, just don't include it in the rows which you're adding up. There isn't an iron law which says that the index of a Java for-loop must start at zero. Write your loop so that it only covers the rows you want to add up.
 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange...I trying to debug my application and getting weird result.

 
John Joe
Ranch Hand
Posts: 430
3
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I click the JSpinner first time(1), I get in else . Fine
When I click the JSpinner second time(2), I get
in else
in else
inside if statement


Why I would not getting inside if else ? Is it because of the return ? But if I remove the return, it will keeps on adding items in new rows
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, is this ever an example of why bad formatting is a bug.  Here's your code formatted correctly[1]

Does the SOP line that says "in if else" make any sense now?

[1] There are different styles to format with -- this isn't the only one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!