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

Error in Eclipse after 'Introduction to Arrays' changes

I finished followed along with this video and made the designated changes. Everything seemed to be going fine until the very end. I think I accidentally deleted something small but important and now I have several errors. I cross referenced my code with the displayed code in the video, but I cannot discern the problem. I am pasting my code from Eclipse below. I am placing an arrow along with the error statement next to where the error is found. Please help!

package com.example.crystalball;

import java.util.Random;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class MainActivity extends Activity { <-- Says that I need to insert "}" to complete the Class body. I'm not exactly sure where to place the closing }.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Declare our View variables and assign them the Views from the layout file
    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1); 

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String[] answers = {
                    "It is certain",
                    "It is decidedly so",
                    "All signs say YES",
                    "The stars are not not aligned",
                    "My reply is no",
                    "It is doubtful",
                    "Better not to tell you now",
                    "Concentrate and ask again",
                    "Unable to answer now", 
                    "It is hard to say" };
            }
            // 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(answers.length); <-- Error under the semicolon. Tells me that I need to use a semicolon to end the statement. Also, the word 'length' does not appear in blue. When I use the dot after answers, it does not automatically go to Intellisense. When I do get it there and select length, it places parentheses which are not needed.

            /* Convert the randomNumber to a text answer
             * 0 = Yes
             * 1 = No
             * 2 = Maybe
             */

            answer = answers[randomNumber];

            //Update the label with our dynamic answer
            answerLabel.setText(answer);


@Override
public boolean onCreateOptionsMenu(Menu menu)  {
    getMenuInflater().inflate(R.menu.activity_main, menu); <-- Error under the semicolon. Tells me that I need to use a semicolon to end the statement.
    return true;
}

}

3 Answers

Hello,

I am not 100% sure but I'm pretty sure that you have an extra curly brace when defining your string array.

 @Override
        public void onClick(View v) {
            String[] answers = {
                    "It is certain",
                    "It is decidedly so",
                    "All signs say YES",
                    "The stars are not not aligned",
                    "My reply is no",
                    "It is doubtful",
                    "Better not to tell you now",
                    "Concentrate and ask again",
                    "Unable to answer now", 
                    "It is hard to say" };
            } //I THINK THIS IS THE EXTRA CURLY BRACE. TRY REMOVING IT AND SEEING WHAT HAPPENS. (Sorry for caps, making it stand out :D)


            // The button was clicked, so update the answer label with an answer
            String answer = "";

You may find you still have errors after this, but let me know how you get on.

Paul :)

Thanks so much for the fast reply! I actually just started fresh and redid everything, lol. Thanks though :)

No problem :)