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

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,546 Points

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

Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

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

Steve.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,546 Points

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.

Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

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

Enyang Mercy
PRO
Enyang Mercy
Pro 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 + ""
Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

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
PRO
Enyang Mercy
Pro 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

Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

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

Steve.

Enyang Mercy
PRO
Enyang Mercy
Pro Student 2,339 Points
Random randomGenerator = new Random();

int randomNumber = randomGenerator.nextInt(10);

String intAsString = randomNumber + "";

i remove the import as u instructed

Steve Hunter
Steve Hunter
Treehouse Moderator 57,546 Points

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