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 with Java Basic Android Programming Using the Random Class

Takudzwa Chauruka
Takudzwa Chauruka
488 Points

need help with android

am to convert an int to a String

RandomTest.java
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(10);
String intAsString = new String(randomNumber);

5 Answers

Hi there,

Just add an empty string, "", onto randomNumber and assign that into intAsString. So, start by declaring String intAsString as you have done, then to the right of the equals, perform the addition.

Steve.

Takudzwa Chauruka
Takudzwa Chauruka
488 Points

hey Steve

am kind of lost there. if you can do an example that will be great. thanks

Takudzwa

Sure! First, declare your String as you have done. Then after the equals sign put randomNumber + "";.

Steve.

I've just seen your other post. You are on a completely different course to the one this thread refers to! You're doing the Kotlin course. That'll be why this thread isn't helping you.

I have answered your question in the correct thread with a solution to the code challenge.

Enyang Mercy
PLUS
Enyang Mercy
Courses Plus Student 2,339 Points

Steve please help. Code won't run!!!!!!!!!!!!!!!!!!

import java.util.Random

Random randomGenerator = new Random();

int randomNumber = randomGenerator.nextInt(10);

String intAsString = randomNumber + ""

You need a semi-colon at the end of the last line. And remove the import line - you don't need that as the challenge provides the necessary libraries.

Steve.

Enyang Mercy
PLUS
Enyang Mercy
Courses Plus Student 2,339 Points

Thanks so much Steve still won't compile. I'm so fed up, i have tried over 4 different methods ERROR; 'random number' should be a string

Can you post your code again - I'll see what the issue is.

Steve.

Enyang Mercy
PLUS
Enyang Mercy
Courses Plus Student 2,339 Points
Random randomGenerator = new Random();

int randomNumber = randomGenerator.nextInt(10);

String intAsString = randomNumber + "";

i remove the import as u instructed

That code is fine and passes the challenge. Maybe refresh the browser.