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

Jeremy Martin
PLUS
Jeremy Martin
Courses Plus Student 2,521 Points

What's wrong with my syntax?

Using random.java... I am trying to declare a String variable named intAsString, convert an Integer named randomNumber into a String and then store that String in intAsString. This is what I entered into the Challenge editor prompt:

intAsString = Integer.toString(randomNumber);

Getting "Bummer!" every time. Is this correct?

4 Answers

Err, sorry. I meant declare intAsString as a String.

String intAsString = Integer.toString(randomNumber);
Jeremy Martin
Jeremy Martin
Courses Plus Student 2,521 Points

Ooooh, java does not automatically declare the type as String if not previously specified?

Nope. Java is a strongly typed language, which means you have to explicitly declare all of your variables and their types.

Jeremy Martin
Jeremy Martin
Courses Plus Student 2,521 Points

That was exactly it! I declared intAsString as a String and it worked. Thank you, Ben!

You need to declare the variable's data type as int (assuming you didn't already declare the variable on a separate line) and put a semicolon at the end of your line.

Jeremy Martin
Jeremy Martin
Courses Plus Student 2,521 Points

Yes, my apologies. This is the entire code:

Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
intAsString = Integer.toString(randomNumber);
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

I will be adding compiler errors to existing Android challenges in the coming weeks that should help with these issues. :-)