简体   繁体   中英

How to convert string to int in this specific situation?

The following piece of code:

if (e.getSource() == btnRe) {
  lblCounter.setText(count - count);
}

Throws the exception:

Exception: Incompatible tpyes: int cannot be converted to String "count - count"

I don't know how to change count into an integer that the method setText can read.

count - count is 0 . You could use

lblCounter.setText(String.valueOf(count - count));

or just

lblCounter.setText("0");

If you concatenate "" to the equation, java will know you're passing a string. If count is a String , this should do the trick.

    if (e.getSource() == btnRe) {

        lblCounter.setText("" + (Integer.parseInt(count) - Integer.parseInt(count)));
    }

Or, if it is an int , then use this.

    if (e.getSource() == btnRe) {

        lblCounter.setText("" + (count -count));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM