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 trialDaniel Moravec
236 PointsCreate a String variable called randomNumber, and set it equal to a random Integer between 0 and 9.
I was learning Android development with Kotlin at Treehouse and there was this question: Create a String variable called randomNumber, and set it equal to a random Integer between 0 and 9. I would write it as:
import java.util.Random
val randomNumber = Random(10)
But this does not work and it keeps telling me: "randomNumber should be a String". Can anybody please help me solve this problem?
import java.util.Random
val randomNumber = Random()
3 Answers
Sai Krishna Katkam
Courses Plus Student 1,946 PointsRandom is just a class.
randomNumber is an variable of String type.
val randomNumber: String = Random()
nextInt() is a method/function in the Random class for generating a random number between N numbers. (here N=10)
val randomNumber: String = Random().nextInt(10)
toString() is a method/function in the Object class for returning a String representation of the object. (Since nextInt() would return an integer, but whereas randomNumber is a String variable, we use toString() method.)
val randomNumber: String = Random().nextInt(10).toString()
janet178
4,220 PointsThanks guys! I am watching the same tutorial. I was so close yet so far!
jose oswaldo guerrero valencia
1,300 Pointshi guys , i have this code , import java.util.Random
val randomNumber:String val randomGenerator: Random()
val randomNumber: randomGenerator.nextInt(10)
Im having trouble to do the exercise?
Daniel Moravec
236 PointsDaniel Moravec
236 PointsThanks a lot, now I get it! :)