Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (retired 2014) Learning the Language Objects and Random Numbers

Richard Davenport
Richard Davenport
1,663 Points

Problems with task 4 of 4 in code challenge

So, I have 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."

Here is my code so far:

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

Does anyone have a moment to guide me on how to correct this?

Thanks in advance, Richard

3 Answers

Stone Preston
Stone Preston
42,016 Points

answer = Integer.toString(intAsString);

the challenge asks for the string to be named intAsString. it also needs a datatype in front of it. Also you pass in the integer you want as a string to the toString method.

String intAsString = Integer.toString(randomNumber);
Richard Davenport
Richard Davenport
1,663 Points

You sir are an incredible help, thank you so much for your answers.

Stone Preston
Stone Preston
42,016 Points

no problem. You almost had it right, just a little flip flopped.

Richard Davenport
Richard Davenport
1,663 Points

Well, I'm extremely new to coding and I tend to overthink things. My friend who already knows code gets confused looking at my lines and then watches the instructional clip and cracks up at how simple it should have been compared to what I do.

Stone Preston
Stone Preston
42,016 Points

yeah that happens a lot. If you struggle with a challenge its best to watch the video one more time. That will usually clear things up.