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 trialSiddhant Choyal
Courses Plus Student 644 PointsI am stuck on this question please help me!
I'm not able to find out my mistake, please help me.
Random randomGenerator = new Random();
String intAsString;
intAsString randomNumber = ;
randomNumber = randomGenerator. nextInt(10);
1 Answer
Gunjeet Hattar
14,483 PointsHi Siddhant
Lets break it up as per challenge
Challenge 1
Initialize a new Random variable called randomGenerator using the Random() constructor. Hint: Don't forget to use the new keyword.
Which I believe you have already done
Random randomGenerator = new Random();
Challenge 2
Create a new int variable named randomNumber. Use the randomGenerator variable and its nextInt() method to get a random number. Do not set a range.
int randomNumber = randomGenerator.nextInt();
Challenge 3
Now limit the range of the randomNumber variable from 0 to 9. Use the version of nextInt() that has a parameter that specifies how many numbers to choose from
nextInt() function also takes a parameter there you can set the number of random variables it can generate
int randomNumber = randomGenerator.nextInt(10)
Why 10? Because n = n+1 ; if value is set to 9 through 0, it will always be an additional 1 in the range.
Challenge 4
Create a String variable named intAsString. Convert the randomNumber integer to a String value and store it in the intAsString variable. Hint: "Moosh" them together like in the video
If you would have had looked at the video, it takes about the Object type integer as opposed to primitive type int
String intAsString = Integer.toString(randomNumber);
Hope that helps.
Siddhant Choyal
Courses Plus Student 644 PointsSiddhant Choyal
Courses Plus Student 644 PointsThanks soo much Gunjeet, you really helped me the most. Thanks a tone. Love u bro, u r awesome :)