Getting JTable value as integer?
I do not understand; I am using DefaultTableModel
and my attempt was to get the value in the table as int
:
Integer.parseInt( tableModel.getValueAt(i, 1) );
Eclipse states that there is a need to render from Object
to String
, so eclipse does this:
Integer.parseInt( (String) tableModel.getValueAt(i, 1) );
The program crashes at runtime because it is impossible to pass "int to string". What for? I was expecting "object -> string -> int".
Try using:
Integer.parseInt( tableModel.getValueAt(i, 1).toString() );
Just set the string representation of the object with toString()
.
The value you get with tableModel.getValueAt(i, 1)
is already Integer
. Just apply it appropriately: Integer value = (Integer) tableModel.getValueAt(i, 1);
(or int
if you want to use a primitive type).