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

Android Build a Simple Android App (2014) Basic Android Programming Using the Random Class

Brian Tong
Brian Tong
1,981 Points

Stuck on 4th question to declare a string variable and mush it together

what is the correct declaration?

RandomTest.java
Random randomGenerator = new Random ();
int randomNumber = randomGenerator.nextInt (10);
string intAsString = randomNumber + "";

2 Answers

Jesse Devaney
Jesse Devaney
6,632 Points

first off, String declarations are capitalized like so, String intAsString; To get the string value of an integer, you can do String intAsString = randomNumber + "";

or you can use the string method valueOf to get the value of the int as a string representation.

For Example, String intAsString = String.valueOf(randomNumber);

I would recommend the first solution where you concatenate the empty string to the number. If you do use valueOf, make sure you call it off of the static String class, like:

String intAsString = String.valueOf(randomNumber);

// or

String intAsString = Integer.toString(randomNumber);