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 trialmilishrivastava
879 PointsConvert the randomNumber integer to a String value and store it in the intAsString variable. Hint: Add them together lik
Convert the randomNumber integer to a String value and store it in the intAsString variable. Hint: Add them together like in the video!
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = "";
intAsString = intAsString[randomNumber];
2 Answers
Vrezh Gulyan
11,161 PointsThe above answer is the usual way most people do it. Its called string concatenation. It is used commonly but is considered bad practice as java has methods to do what you want :
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
// option 1
String intAsString = Integer.toString(randomNumber);
// option 2
intAsString = String.valueOf(randomNumber);
jcorum
71,830 PointsMili, you start off fine! But they just want you to store the random number you create in a String. Since you can't do that directly (Java won't do the conversion and you can't cast it) you need to use the trick used in the video. You concatenate it with a String. If you add a number to a String you get a String. 2 + "" = "2"
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = randomNumber + "";
Hope this helps.