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 (retired 2014) Learning the Language Objects and Random Numbers

Sergio Segovia
PLUS
Sergio Segovia
Courses Plus Student 312 Points

Help 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

String = "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
KUNAL HAJRA
5,288 Points

Could you please post your questions a bit more clearly? I am not able to understand what is your issue.

Kiran Rambha
Kiran Rambha
2,320 Points

You have a mistake in your String declaration... It should be

String intAsString = Integer.toString(randomNumber);

Glad I could help. See you around!