Vestel
Vestel

Reputation: 51

int cannot be converted to string?

I am trying to write the instructions to notice when circle is about to be 12 and then add 1 to square.

The code below works fine for me

   int x = Integer.parseInt(circle.getText()); 
   x = x + 1;
   String z = Integer.toString(x);
   circle.setText(z);

However, I am having trouble with these new instructions I am trying to write. How can I get square, convert to integer, add 1 and then put the value back?

   int q = Integer.parseInt(square.getText());
   x = q + 1;
   square.setText(x);

Upvotes: 3

Views: 30431

Answers (4)

Krusty Simancas
Krusty Simancas

Reputation: 1

Using your own code String z = Integer.toString(x); square.setText(z);

Upvotes: -1

Don Chakkappan
Don Chakkappan

Reputation: 7560

square.setText(x+"")

Will work

Upvotes: 2

Dici
Dici

Reputation: 25950

Why would an int be convertible to a String (unless an implicit conversion exists, which is not the case, there is not a lot of implicit things with the Java compiler, primitive wrappers and string concatenation apart) ?

Use one of these two options :

square.setText(String.valueOf(x));
square.setText(x + "");

Upvotes: 0

Eran
Eran

Reputation: 393771

You can convert to String using Integer.toString() :

square.setText(Integer.toString(x));

Upvotes: 8

Related Questions