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

JavaScript

Ben Bellis
Ben Bellis
2,017 Points

Stuck on the Random generator

On the challenge after the Random generator part for it asks me to

"Declare a String variable named intAsString. Convert the randomNumber integer to a String value and store it in the intAsString variable. Hint: Use the toString() method of the Integer class."

It's thrown me off completely as I try to enter

Random randomGenerator = new Random(); int randomNumber = randomGenerator.nextInt(10); answer = Integer.toString(intAsString);

I feel although I'm not learning what I'm supposed to and it feels like I'm just copying what the instructor does on his screen, so when I get asked this I'm completely unsure of what to do.

Yes, I know the feeling. Some sections I am able to get through, but this certainly knocked me for 6. I must of been adding different variations for the last 2 hours. Yes, crazy I know. Don't worry Ben. We will get there. Trust in Treehouse. :-)

2 Answers

Patrick Cooney
Patrick Cooney
12,216 Points

If I'm reading the question right I think what you're looking for is String intAsString = Integer.toString(randomNumber);

// here you're initializing the class
Random randomGenerator = new Random();

// this gets your random int and saves it to an int variable
int randomNumber = randomGenerator.nextInt(10);

/*this line creates a String variable called intAsString which will hold a string
representation of your int. You are calling the toString() on the Integer class
to which you should be passing an integer to be converted into a string.
In this case that integer is contained within the randomNumber variable */
String intAsString = Integer.toString(randomNumer);

Thank you for your help and advice Patrick. I think I know how to take on the challenge now. Very useful tips there Sir.

I got stucked too. Sorry but I think that the question isn't explained very well, so maybe try fix this for any future problems.

Rachel Anamaria
PLUS
Rachel Anamaria
Courses Plus Student 928 Points

I got stucked too. Sorry but I think that the question isn't explained very well, so maybe try fix this for any future problems.