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 (2014) Basic Android Programming Using the Random Class

satish singla
satish singla
906 Points

I am not able to solve this challenge

Challenge Task 4 of 4

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.

1 Answer

Rebecca Rich
PLUS
Rebecca Rich
Courses Plus Student 8,592 Points

You can convert an integer in Java to a String by "Moosh"-ing or concatenating it together with an empty String (""). So for example you could try:

String intAsString = randomNumber + "";

The + operator is a shorthand in Java for the "Moosh"-ing or concatenating operator, so what this is doing is taking the randomNumber int and "moosh"-ing it with an empty String. This will result in a String where the text is that of the randomNumber. So for example, if randomNumber = 3, intAsString would be "3" after this statement.

This works for all Strings in general not just empty ones (so for example, I could code:

String intAndStringExample = randomNumber + "woohoo";

and expect that if randomNumber is an integer equal to 3 that intAndStringExample = "3woohoo".)

Hope this helps!