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 trialSergio Segovia
Courses Plus Student 312 PointsHelp please D:
What am i doing wrong in this test
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
Random randomGenerator = new Random (); int randomNumber = randomGenerator.nextInt(10); String = "intAsString"; Integer.toString(intAsString);
4 Answers
Gunjeet Hattar
14,483 PointsString = "intAsString";
Integer.toString(intAsString);
This is an invalid Java statement
Lets take a closer look
In the first line String keyword is not followed by a variable name, which is an error. By convention in java we always have datatype variable_name , where datatype could be int, float, String etc.
In the second line, the toString method takes an integer value and not a String value. If you come to think about it, if something is already string why would you convert it to string all over again.
Here is a quick look at fixing the errors
Random randomGenerator = new Random ();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = Integer.toString(randomNumber); //pass the int variable as parameter
Hope that helps
KUNAL HAJRA
5,288 PointsCould you please post your questions a bit more clearly? I am not able to understand what is your issue.
Kiran Rambha
2,320 PointsYou have a mistake in your String declaration... It should be
String intAsString = Integer.toString(randomNumber);
Sergio Segovia
Courses Plus Student 312 PointsThanks everyone! Gunjeet you rock :)
Gunjeet Hattar
14,483 PointsGlad I could help. See you around!
Sergio Segovia
Courses Plus Student 312 PointsSergio Segovia
Courses Plus Student 312 PointsThanks you answer waas right