Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert an int to a String? [duplicate]

Tags:

java

string

People also ask

Can you turn an int into a double?

We can convert int value to Double object by instantiating Double class or calling Double. valueOf() method.

Can we convert int to string in java?

In Java, String. format() is a new alternative method that can be used for converting an Integer to a String object. Though the purpose of this method is to format a string, it can also be used for conversion.


Use String.valueOf():

int sdRate=5;
//text_Rate is a TextView
text_Rate.setText(String.valueOf(sdRate)); //no more errors

Use the Integer class' static toString() method.

int sdRate=5;
text_Rate.setText(Integer.toString(sdRate));

You can use

text_Rate.setText(""+sdRate);

Did you try:

text_Rate.setText(String.valueOf(sdRate));

You have two options:

1) Using String.valueOf() method:

int sdRate=5;
text_Rate.setText(String.valueOf(sdRate));  //faster!, recommended! :)

2) adding an empty string:

int sdRate=5;
text_Rate.setText("" + sdRate)); 

Casting is not an option, will throw a ClassCastException

int sdRate=5;
text_Rate.setText(String.valueOf((String)sdRate)); //EXCEPTION!