0

Im getting a syntax error with the semi at the end of my array and the last closing bracket. I've been banging my head over this for a bit. Does anyone have any ideas on how to resolve this?

public class arrayWords {
    private Words wl;
    private String gamewords[];

    public arrayWords() {
        wl = new Words();
        gamewords = new String[] { "dog", "cat", "coffee", "tag", "godzilla", "gamera", "lightning", "flash",
                "spoon", "steak", "moonshine" };
        setArray();
    }

    public void setArray() {
        for (int i = 0; i < gamewords.length; i++)
            wl.wordList[i].setWord(gamewords[i]);
    }
}

public class Words {
    public Word wordList[];
    public Words() {
        super();
        wordList = new Word[25];
        for (int i = 0; i < 24; i++) {
            wordList[24] = new Word();
        }
    }
}

public class Word {

    String name;
    Words word = new Words();

    public Word() {
        super();
        name = new String("");
    }

    public Word(String w) {
        super();
        name = new String(w);
    }

    public String getWord() {
        return name;
    }

    public void setWord(String s) {
        this.name = new String(s);
    }

}

public class wordTest {
    public static void main(String args[]) {
        Words wl = new Words();
        arrayWords newArray = new arrayWords();

        newArray.setArray();

        for (int i = 0; i < 25; i++)
            System.out.println(wl.wordList[i].getWord());
    }
}
10
  • 3
    place your for loop in a method Commented Feb 22, 2015 at 3:03
  • Next time, please format your code, before pasting it here. Makes it more readable for everyone. Commented Feb 22, 2015 at 4:06
  • for (int i = 0; i < 24; i++) { wordList[24] = new Word(); } probably isn't doing what you want it to do. Perhaps i < 25 and wordList[i] = .... Commented Feb 22, 2015 at 4:10
  • Also, Word constructs a WordList when it is constructed which then constructs 25 Words so either class's constructor will blow out the stack. Commented Feb 22, 2015 at 4:11
  • What is a good way around this? Commented Feb 22, 2015 at 4:18

1 Answer 1

4

The problem isn't the array. You are trying to run a for-loop right inside the class. This should be done in either a method or the main-method.

Edit:

An example of what you might want:

public class ArrayWords {
private Words wl;
private String gamewords[];

public ArrayWords() {
    wl = new Words();
    gamewords = new String[] { "dog", "cat", "coffee", "tag", "godzilla", "gamera", "lightning", "flash", "spoon", "steak", "moonshine" };
    setArray();
}

public void setArray() {
    for (int i = 0; i < gamewords.length; i++)
        wl.wordList[i].setWord(gamewords[i]);
}

// getters and setters
}

Now you can simply have a class with a main-method and initialize it as

ArrayWords arrayWords = new ArrayWords();
arrayWords...... // what you want to do
Sign up to request clarification or add additional context in comments.

4 Comments

Could i just write a set method? void setArray(){ for (int i=0; i<gamewords.length; i++) wl.wordList[i].setWord(gamewords[i]); }
Given that the full code is not provided, it's hard for me to determin where it best fits in. But surely, you can have a method like you mentioned and call it from wherever your program is initiated. Please provide full code if you want comments on your approach.
codepublic class arrayWords { Words wl = new Words(); private String gamewords[] = {"dog","cat","coffee","tag", "godzilla", "gamera", "lightning","flash","spoon","steak","moonshine"}; void setArray(){ for (int i=0; i<gamewords.length; i++) wl.wordList[i].setWord(gamewords[i]); }code
@JohnCasey please update your main question if you have more code. It's fairly hard to read it in the comments. I updated my answer with an example, I hope thats what you're after.

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.