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".

+3


source to share


2 answers


Try using:

Integer.parseInt( tableModel.getValueAt(i, 1).toString() );

      



Just set the string representation of the object with toString()

.

+5


source


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).



+2


source







All Articles