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 (2014) Basic Android Programming Using the Random Class

Stuck AGAIN!

Help!

RandomTest.java
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
intAsString =  ;

3 Answers

Devin Scheu
Devin Scheu
66,191 Points

Your code should look like this:

Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = randomNumber + "";

What's going on here is that the code need the random number to be stored as a string, not an int, for some reason unknown to us in the code you've shown. An easy way to do this is to take a int value and add a space to the end of it. This looks like:

8 + ""

This will make the integer value into a string, so you can use it with methods and classes that require strings. If this helps, please rate it and let me know! Happy Coding!

Nicolas

Or the conventional ways would be by using the Integer.toString(int i) or String.valueOf(int i). in this case: String intAsString = Integer.toString(randomNumber); or String intAsString = String.valueOf(randomNumber);