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 trialEnyang Mercy
Courses Plus Student 2,339 PointsAndroid string randomNumber ERROR: 'randomNumber' should be string
Stuck for a week!!!!!!!!!!!!!!!!!!!!!!! please help urgently.
import java.util.Random
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = randomNumber + ""
3 Answers
Enyang Mercy
Courses Plus Student 2,339 PointsThanks Nicos but code won't run. I have been stuck here for over a week. Just paying money without studying. Awkward feeling
Nikos Tzouvelekis
8,155 Pointsyes sorry i don't realize that it's Kotlin language :) ι just run the challenge and the answer is like just like Steve's answer :) be careful when stuck just read again the exercise :) also you can search at the google etc
Steve Hunter
57,712 PointsHi there,
Now we're talking about the right course, I can help you out! You posted in the other thread which wasn't a Kotlin course at all.
Don't delete the import
; leave that in. So, here you want to create a string variable called randomNumber
. That looks like:
val randomNumber: String = Random();
There's a method called nextInt()
in the Random class which generates random numbers with the range passed in as the argument. Use the value 10 for this:
val randomNumber: String = Random().nextInt(10);
But we want to store the value as a string, not a number so chain the .toString()
method onto the end:
val randomNumber: String = Random().nextInt(10).toString();
That should get you through the challenge.
Steve.
Enyang Mercy
Courses Plus Student 2,339 PointsThanks guys. U have truly helped and i apologize for the inconveniences.
Nikos Tzouvelekis
8,155 PointsNikos Tzouvelekis
8,155 PointsString intAsString = String.valueOf(randomNumber);
Here :) this converts int to String and i see also at end of the line you must add semicolon ---> ;
you have write : String intAsString = randomNumber + ""
you must write : String intAsString = randomNumber + "" ;
but the better way its this with: String.valueOf(int);
:)