• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

model.getValueAt(1,2);

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm probably over looking something simple here but I am having problems with the method getValueAt() in the TableModel class. The problem is that it returns Objects and I want to add up the sum of primitive data types,int. Not sure how to approach this any help would be appreciated.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If those objects happen to store ints, then you can get the ints out and add them up. If the objects you put in don't store ints, then there are no ints there to add anyway.

For instance, if you put in Integer objects (possibly without knowing it by passing an int primitive to something that autoboxes(⇐click) it for you), then you can call Integer's intValue() method to get the int out, or just let auto-unboxing do it for you.

If you put in some other object that stores an int, then you'll have to get that int out by whatever means that object provides. For instance maybe it's a Person with a height, that has a getHeight() method.

If the method return Object and does not use generics(⇐click), then you'll have to cast. For example:


I think auto-ubox might even let you do:

 
Shelby Simpson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help. I did try cast typing but I keep getting this error -
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

Here is the code I'm working on -
 
Rancher
Posts: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer



Don't add a String to the model. Instead add an Integer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic