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) Improving Our Code Simple Refactoring: Using a Class

"Cannot resolve symbol Random" in the FactBook

Hi there,

I have gotten as far as doing all the steps to create a new class called FactBook (or in my app, QuestionLog), cut and paste the code governing what the text content of the random questions is, and tidy up the code to the end of this video.

But as soon as I cut and pasted the code over, which included the code instructing the random generator to pick a line of fact text from within the array above, everywhere "Random" was mentioned in that piece of code glowed red and the error over it says "cannot resolve symbol Random".

Ben hasn't given a resolution since his code didn't do this - did I do something wrong? here's the code for the new class:

***/**
 * Created by Chiba on 2015-11-08.
 */
public class QuestionLog {
    // Member variable (properties about the object)
    public String [] mQuestions = {
            "Given the choice of anyone in the world, whom would you want as a dinner guest?",
            "Would you like to be famous? In what way?",
            "Before making a telephone call, do you ever rehearse what you are going to say? Why?",
            "What would constitute a perfect day for you?",
            "When did you last sing to yourself? To someone else?",
            "If you were able to live to the age of 90 and retain either the mind or body of a 30-year-old for the last 60 years of your life, which would you want?",
            "Do you have a secret hunch about how you will die?",
            "Name three things you and your partner appear to have in common.",
            "For what in your life do you feel most grateful?",
            "If you could change anything about the way you were raised, what would it be?"};

    // Method
    public String getQuestion() {

    String question = "";

        //Randomly select a question
    Random randomGenerator = new Random(); //Construct a new Random number generator
    int randomNumber = randomGenerator.nextInt(mQuestions.length);

    question = mQuestions[randomNumber];

    return question;
    }
}

2 Answers

You need to organise your imports.

Go to Code | Optimise Imports ... or try Ctrl+Shift+O.

Steve.

Thank you, Steve. I was experimenting and found the error went away when I retyped all the code dealing with setting up a new random number generator e.g. all after //Randomly select a question. Is that a clumsy way of doing the same thing? I'll remember this.