0

So I'm working on my application and I'm simply trying to declare a new array of strings. For some reason it wants an extra bracket to close the class at the end (even though the brackets are fine), and also after "private String[] addSentences = new String[3];" if asks for "{" instead of ";". In other words it wants to close something...I don't get it. Maybe you guys can help.

package org.chinesetones.teacher;

import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;
import org.chinesetones.teacher.Sentence;

public class Game extends Activity implements OnClickListener {
private String[] addStrings = new String[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

// Setup button listeners...
View nextButton = findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
View repeatButton = findViewById(R.id.repeat_button);
repeatButton.setOnClickListener(this);
}
public void onClick(View v){
    switch(v.getId()) {
    case R.id.next_button:
        giveSentence();
        break;
    case R.id.repeat_button:
        playSentence();
        break;
    }
}
private ArrayList<Sentence> sentences;
private String[] addSentences = new String[3];
addSentences[0] = "Hi";
addSentences[1] = "No";
addSentences[2] = "Yes";
}

giveSentence() and playSentence() have not been created yet. The errors are below.

Description Resource Path Location Type Syntax error on token ";", { expected after this token Game.java /ChineseTones/src/org/chinesetones/teacher line 39 Java Problem

Description Resource Path Location Type Syntax error, insert "}" to complete ClassBody Game.java /ChineseTones/src/org/chinesetones/teacher line 43 Java Problem

Thanks!

2 Answers 2

1

You cannot initialize class field that way..

Change

private String[] addSentences = new String[3];
addSentences[0] = "Hi";
addSentences[1] = "No";
addSentences[2] = "Yes";

to

private String[] addSentences = {"Hi", "No", "Yes"};

The other option is to just do

private String[] addSentences = new String[3];

and initialize the array in the class constructor.

public Game()
{
   addSentences[0] = "Hi";
   addSentences[1] = "No";
   addSentences[2] = "Yes";
   ...
}
Sign up to request clarification or add additional context in comments.

4 Comments

okay thank you, if I were to have around 500 indexes for this array though, would I have to manually add them like this or is there another way?
@Wes see if my edited answer helps. You can init the array elements in the constructor.
If the values (strings u are storing) are different(like in your case), then you would have to manually add them. If there is some form of formula which generates the value needs to be stored, then you can run a loop over array.length() and store them.
My problem is they are sentences, and every sentence is different, so I guess my only option is to manually type out all the sentences to be used (probably more than 200). Can't think of another way. Thanks for the help.
0

You can only have declarations outside of methods. So your addSentence assignements

addSentences[0] = "Hi"; 
addSentences[1] = "No"; 
addSentences[2] = "Yes"; 

must either be in a method, constructor, or be part of the declaration.

For a small, statically-defined list like your's, you can do this:

 private String[] addSentences = {"Hi","No","Yes"}; 

If you had a larger initialization list you put that logic into your constructor.

public Game(){
   addSentences[0] = "Hi"; 
   addSentences[1] = "No"; 
   addSentences[2] = "Yes"; 
   ....
}

2 Comments

that's everything mentioned in the first answer. Did you not see it?
Wow, nearly identical. I must've missed it as I was typing my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.