Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App Basic Android Programming Using the Random Class

subhanil chakraborty
subhanil chakraborty
117 Points

how to convert int into string?

it is asked to convert int randomNumber into a string and store it into intAsString variable.

RandomTest.java
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString;
String fact = "";
fact = intAsString[randomNumber];

2 Answers

Hi subhanil,

You don't have to create another variable to complete this task.

The hint that's mentioned for the video showed you that you can convert a number to a string by adding an empty string to it.

String intAsString = randomNumber + "";

So all you need to do is add an empty string to the random number and then store it in the intAsString variable.

Chris Adamson
Chris Adamson
132,143 Points

Use the valueOf static method from the String class:

fact = String.valueOf(randomNumber);