Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.