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 trialPeter Heide
583 PointsChallenge Task 4 of 4
Challenge Task 4 of 4
I am not very clear on the instruction
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
int intAsString = randomNumber = "";
2 Answers
Allison Hanna
36,222 PointsHi there, For the last question, "Declare 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!"...
First we declare a String:
String
then we name it intAsString:
String intAsString
then we use the equals sign to denote that we are setting that variable to something:
String intAsString =
then we "set it to something", in this case converting randomNumber, an integer, to a String, starting with the data type, Integer...
String intAsString = Integer
then we add a period to call the method "toString" which converts that int to a String:
String intAsString = Integer.toString();
and finally we pass in the parameter of our variable, randomNumber:
String intAsString = Integer.toString(randomNumber);
That last line of code should work. I'm still learning as well, so if someone else reads this and finds a mistake or can suggest a better way to articulate things, please let me know. Thanks!
Jason Anello
Courses Plus Student 94,610 PointsHi Peter,
You're close.
The intAsString
variable should have a String
datatype, not an int
Also, you put a second equal sign which should have been a plus sign. You want to add the number to the empty string so that the number will be converted into a string.
String intAsString = randomNumber + "";
Elie Haykal
8,163 PointsHi Peter,
Can that method be considered as a best practice ?
Thanks
Peter Heide
583 PointsPeter Heide
583 PointsThank you