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
Alexander Mann
285 PointsCrystal Ball App - "answers cannot be resolved to a variable"
@Override public void onClick(View arg0) { String [] answers = { "It is certain", "It is decidedly so", "All signs say YES", "The stars are not alligned", "My reply is no", "It is doubtful", "Better not tell you", "Concentrate and ask again", "Unable to answer now"}; } // 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(); //Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(answers.length);
answer = answers[randomNumber];
//Update the label with our dynamic answer
answerLabel.setText(answer);
}
});
}
I am getting error messages with the 'answers' of answers.length and the ; after the parenthesis. I am also getting error messages for the last three collections of braces/brackets. My options for fixing the answers error are: 1) create field 2) create field in main activity 3) change to answer... etc.
Thank you for your help.
2 Answers
Gunjeet Hattar
14,483 PointsHave you declared the answer string variable's datatype String in the line answer = answer[randomNumber];
It should read String answer = answers[ranomNumber];
I hope I understood your question.
Alexander Mann
285 PointsI have figured it out. Just a missed bracket. Just needed to take an hour away from it. Thank you.