-1

Hi I'm new to Java and I'm having difficulty with creating my word search puzzle program. I'm trying to get a user to enter an ArrayList of words so it can be entered into my puzzle but I keep get an error when I enter an Array of Strings can anybody help please??

 import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    private void createPuzzleGrid()
    {
        int i;
        for(i = 0; i < puzzleWords.size() ; i++){
            letterCount = puzzleWords.size() + letterCount ;
        }
        gridDimensions = letterCount * 2;
        puzzle = new char[gridDimensions][gridDimensions] ;
    }

    private void generateWordSearchPuzzle()
    {

    }

    public void fillPuzzle()
    {
        int i, j, r;
        for (i = 0; i < this.gridDimensions; i++)
            for (j = 0; j < this.gridDimensions; j++)
                if (puzzle[i][j] == ' ') {
                    r = (int) (Math.random() * letterCount);
                    puzzle[i][j] = letterCount.charAt(r);
        }
    }

    public void fill() 
    {
        int i, j, gridDimensions, r, c;
        boolean added;
        this.clear();
        for (i = 0; i < WordSearch.words.size(); i++) {
            ws = (WSWord) WordSearch.words.elementAt(i);
            row = (int) (Math.random() * this.rows);
            col = (int) (Math.random() * this.cols);
            added = false;
        }
user1323808
  • 31
  • 1
  • 11
  • 2
    Post your complete code and the error you receive when trying to execute it. – mcfinnigan Apr 10 '12 at 10:43
  • I have to input an array of words to be loaded into my Puzzle but i keep getting this error when I enter a String like {"hello","bye"}I get this error: Error: illegal initalizer for java.util.ArrayList – user1323808 Apr 10 '12 at 11:02
  • Please post the code that calls the `WordSearchPuzzle` constructor. My guess is that you are trying to pass a `String[]` instead of a list. – Qwerky Apr 10 '12 at 11:08
  • You really need to post your code, otherwise we're unable to see precisely what you're doing. You cannot initialise arraylists with `{}` constructs - that is reserved explicitly for arrays. – mcfinnigan Apr 10 '12 at 11:08
  • Same homework http://stackoverflow.com/q/10192950/211197 – maksimov Apr 17 '12 at 14:21

3 Answers3

0

If you're referring to puzzle as your array of Strings, it isn't, it's a two-dimensional array of char. Perhaps you need

private String puzzle[];

Unfortunately it's not very clear from the given code example what exactly are you having an issue with.

maksimov
  • 5,792
  • 1
  • 30
  • 38
  • I have to input an array of words to be loaded into my Puzzle but i keep getting this error when I enter a String like {"hello","bye"}I get this error: Error: illegal initalizer for java.util.ArrayList – user1323808 Apr 10 '12 at 11:00
  • No, you can't initialise ArrayList with an array. You can try something like: this.puzzleWords = java.util.Arrays.asList(yourStringArray); – maksimov Apr 10 '12 at 11:06
0

You can't just use the = to copy arraylist and its contents: From a similar question: clone(): ArrayList.clone() I thought does a shallow copy

Adapted to your case:

this.puzzleWords = new ArrayList(userSpecifiedWords)
Community
  • 1
  • 1
Pedro Ferreira
  • 629
  • 3
  • 8
  • I don't think this is the issue here. He is allowed to do that but of course changing userSpecifiedWords will cause a change in puzzleWords array as they point to the same array. – giorashc Apr 10 '12 at 10:49
  • What i typed created a clone in a diferent memory position. What he posted above will not copy the contents of the Strings. – Pedro Ferreira Apr 10 '12 at 10:51
0

Initialize puzzleWords.

private ArrayList<String> puzzleWords = new ArrayList<String> ();
titogeo
  • 2,156
  • 2
  • 24
  • 41