0

I want to pass the JLabel to Integer, The following code does not work even with Integer.valueOf() and Integer.parse()

This are the following code I've Tried:

Test 1:

JLabel life = new JLabel("204");
int x = Integer.valueOf(life).intValue();

Test 2:

JLabel life = new JLabel("204");
int x = Integer.parseInt(life);
E.Geek
  • 11
  • 2

1 Answers1

0

No. You can't magically convert a Label to Integer.

However you can get the string of that label and then convert.

JLabel life = new JLabel("204");
int x = Integer.parseInt(life.getText());

Note that, you'll be succeed when there is proper text. For ex

"203", "34343" works but not "A2342"

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307