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 (retired 2014) Learning the Language Objects and Random Numbers

to.String

this is for the answer = Integer.toString(randomGenerator); eclipse shows an error for .toString Please help me out

This is what I have:

public void onClick(View v) {
            //the button was clicked, so update the answer label with an answer
            String answer = "";

            // Randomly select one of three answers, yes, no , or maybe
            Random randomGenerator = new Random();
            int randomNumber = randomGenerator.nextInt(3);
            answer = Integer.toString(randomNumber);     <- Error
            // Update the label with our dynamic answer
            answerLabel.setText(answer);

This is the Error text: The method toString(int) in the type integer is not applicable for the arguments (Random)

2 Answers

Iskandar Fendi
Iskandar Fendi
5,700 Points

Hi David,

What does the error said? I suspect: What is your randomGenerator type? Is it Random class? If it is it will give you an error. .

It is Random class I think

Iskandar Fendi
Iskandar Fendi
5,700 Points

Yes, it is! The error is when you put:

answer = Integer.toString(randomGenerator). In order to fix it, change it into:

answer = Integer.toString(randomNumber);

It will fix the problem.

In your Eclipse, it should tell you why (I assume "Type Don't Match").The reason for your error is:

You put in a Random Class Type instead of Integer.

In programming, you can look up toString() method in Java API

It says: static String toString(int i)

It takes integer type inside of it.

Please let me know the updates

I changed it but the error is still the same. I have the error text in the comments on my question I see what you're saying though. It should work, but it won't

It's working now! Thanks!

are you trying to take a random Number that you generated and save it as a string? if so use:

answer = randomGenerator.toString();

The toString() method should not be taking any parameters, unless you overrode the method. It is normally used: variable.toString()