0

I am trying to access a value in a JTable I have created, and use it in a calculation (simple addition).

String orderSize = (tableModel.getValueAt(2,3)).toString();
// I've tried a few ways to create an integer variable, it is not working here
int orderSizeInt = orderSize.parseInt();
int orderSizeInt2 = orderSize.valueOf(tableModel.getValueAt(2,3));

On the use of parseInt(), I get the error "The method parseInt() is undefined for the type String", which I am confused about.

Additionally, on the second line, I get two more errors. One, "Cannot convert from string to int". The other is that the static method valueOf(Object) from the type String should be accessed in a static way.

I am really confused and would appreciate any help!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) *The method parseInt() is undefined for the type String",* - `parseInt(...)` is a static method of the `Integer` class. 2) Don't store a String in the table. Instead store an `Integer` object in the table. – camickr Jul 27 '21 at 03:28
  • The correct usage of `parseInt` would be `int orderSizeInt = Integer.parseInt(orderSize);` – sorifiend Jul 27 '21 at 03:32
  • @sorifiend Thank you, I got it to work using your advice. I'm not sure how to mark this as the correct answer but thanks for your help! – activeuser2323 Jul 27 '21 at 10:17
  • 2
    Happy to help. You won't be able to accept a comment as an answer, but we can close it off as a duplicate: [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – sorifiend Jul 27 '21 at 22:44

0 Answers0