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 trialJeremy Martin
Courses Plus Student 2,521 PointsWhat'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
Ben Rubin
Courses Plus Student 14,658 PointsErr, sorry. I meant declare intAsString
as a String
.
String intAsString = Integer.toString(randomNumber);
Ben Rubin
Courses Plus Student 14,658 PointsNope. Java is a strongly typed language, which means you have to explicitly declare all of your variables and their types.
Jeremy Martin
Courses Plus Student 2,521 PointsThat was exactly it! I declared intAsString as a String and it worked. Thank you, Ben!
Ben Rubin
Courses Plus Student 14,658 PointsYou 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
Courses Plus Student 2,521 PointsYes, my apologies. This is the entire code:
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
intAsString = Integer.toString(randomNumber);
Ben Jakuben
Treehouse TeacherI will be adding compiler errors to existing Android challenges in the coming weeks that should help with these issues. :-)
Jeremy Martin
Courses Plus Student 2,521 PointsJeremy Martin
Courses Plus Student 2,521 PointsOoooh, java does not automatically declare the type as String if not previously specified?