0

I've been working on a Java project for college for the past few days. I've hit a few bumnps, which I overcame with some help from users on this site. What I am doing is a word search which takes words input by the user as an array list of strings and maps them randomly on a 2-d array. I have created methods for choosing a row and column, for choosing a direction the word goes, and for checking if there are enough clear spaces for said word. I am getting some errors in compiling that code. Here is my previous question ,so as you can see, I've made some progress since then :) https://stackoverflow.com/questions/10193291/adding-words-to-a-2-d-array

Specifically, the problem is that I am trying to reference the doesitfit methods (up, down, left or right) by using a random number to decide which method to choose. It is not compiling :/

    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 static int row, column;




    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)

    {

        this.puzzleWords = userSpecifiedWords ;




    }




    private void createPuzzleGrid()

    {

        int i, itemLength;

        String item;

        for (i = 0; i < puzzleWords.size(); i++) {

            item = puzzleWords.get(i);

            itemLength = item.length();

            letterCount = letterCount + itemLength;

        }

        gridDimensions = letterCount * 2;

        puzzle = new char[gridDimensions][gridDimensions] ;

    }




    private void generateWordSearchPuzzle()

    {




    }




    public void northSouthEastWest(String word)

    {

        int upDownLeftRight, north, south, east, west;

        north = 1;

        south = 2;

        east = 3;

        west = 4;

        String Word;

        upDownLeftRight = (int)(Math.random() * 4);

        if(upDownLeftRight == north){

            fitWordNorth(word);

        }else if(upDownLeftRight == south){

            fitWordSouth(word);

        }else if(upDownLeftRight == east){

            fitWordEast(word);

        }else if(upDownLeftRight == west){

            fitWordWest(word);

        }

    }




    public void firstSpace(String word) 

    {  

        row = (int)(Math.random() * gridDimensions);

        column = (int)(Math.random() * gridDimensions);




        if(puzzle[row][column] != ' ') { 

            firstSpace(word);

        } else {

            northSouthEastWest(word);

        }

    }




    public void fitWordNorth(String word)

    {

        boolean clear = false;

        int p, i; 

        if(row >= word.length()){

            for(i = row - 1; i < word.length(); i--){

                if(puzzle[i][column] != ' '){

                    firstSpace(word);

                }else{

                    clear = true;

                }




                if(clear == true){

                    for(p = 0; p < word.length(); p++){

                        puzzle[row - p][column] = word.charAt(p);

                    }

                }else{

                    firstSpace(word);

                }

            }

        }

    }




    public void fitWordSouth(String word)

    {

        boolean clear = false;

        int row, column, p, i; 

        if(row >= word.length()){

            for(i = row + 1; i < word.length(); i++){

                if(puzzle[i][column] != ' '){

                    firstSpace(word);

                }

                clear = true;

            }

        }else{

            firstSpace(word);

        }

        if(clear == true){

            for(p = 0; p < word.length(); p++){

                puzzle[row + p][column] = word.charAt(p);

            }

        }else{

            firstSpace(word);

        }

    }




    public void fitWordWest(String word)

    {

        boolean clear = false;

        int row, column, p, i; 

        if(column >= word.length()){

            for(i = column - 1; i < word.length(); i--){

                if(puzzle[row][i] != ' '){

                    firstSpace(word);

                }

                clear = true;

            }

        }else{

            firstSpace(word);

        }

        if(clear == true){

            for(p = 0; p < word.length(); p++){

                puzzle[row][column - p] = word.charAt(p);

            }

        }else{

            firstSpace(word);

        }

    }




    public void fitWordEast(String word)

    {

        boolean clear = false;

        int row, column, p, i; 

        if(column >= word.length()){

            for(i = column + 1; i < word.length(); i++){

                if(puzzle[row][i] != ' '){

                    firstSpace(word);

                }

                clear = true;

            }

        }else{

            firstSpace(word);

        }

        if(clear == true){

            for(p = 0; p < word.length(); p++){

                puzzle[row][column + p] = word.charAt(p);

            }

        }else{

            firstSpace(word);

        }

    }

}
2
  • 1
    What compile error do you get? On what line?
    – assylias
    Apr 18, 2012 at 12:27
  • Your variable names should be camel case with the first letter lowercase. Word should be word. Apr 18, 2012 at 12:54

2 Answers 2

1

You are calling fitWordNorth(Word), fitWordSouth(Word), fitWordEast(Word),fitWordWest(Word) functions from your northSouthEastWest() function. The compiler is complaining becuase there is no variable defined as Word in either the class scope or in the method scope.

I also do not see the northSouthEastWest() function being called from anywhere in the program. Please define the function as northSouthEastWest(String Word) for the function to work.

7
  • Thanks. How do I call the row value from firstSpace in fitNorth/firSouth/firEast/fitWest? The programme is complaining as the variable has not been initialised. Apr 18, 2012 at 13:09
  • @ThomasMoore Please let me know the flow of the functions in your program. It is complaining about uninitialized gridDimensions (may be) because you have declared it at the class level, but you may have not set its value. You can set its value in the Contructor, or may be some function. It will be nice to have the main(String[] args) function with the flow of the functions being called. Apr 18, 2012 at 13:57
  • @ThomasMoore Looking further, I think you need to do something like int row=0, column=0, p, i; in the functions. You need to initialize (assign a value) a variable before using (reading) it. In your program, you have declared row and column as int variables, but you have not given them a value before reading it. Think about it logically. You can not read a value before actually it having a value. Apr 18, 2012 at 14:04
  • I understand that. That was a mistake. Row and column are randomly initialised in a previous method. My problem now is how to use "row" and "column" in a seperate method. How can I call those values from another method? Apr 18, 2012 at 14:17
  • @ThomasMoore There are two ways you can access the value of a variable 1. by passing the value through parameters. 2. declare the variable as class level variable. You can have row and column as class level variables. Please look at the suggestions that I have posted in a second answer. Apr 18, 2012 at 14:30
0

After looking further in to your code, there can be many suggestions. I will update this answer as an when I get time. You may have a main method.. something like

/**
     * @param args
     */
    public static void main(String[] args) {
        if(null!=args && args.length>0){
            WordSearchPuzzle wsp = new WordSearchPuzzle(Arrays.asList(args));
        }else{
            System.err.println("Please enter words to search");
        }
        //TODO
        //Call your functions in the series that you want...
    }

You constructor can be like

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

Define your puzzleWords variable as List and not ArrayList

 private List<String> puzzleWords ;

Your northSouthEastWest function can be like: (north, south, east, and west can be constants)

    public static final int NORTH= 0;
    public static final int SOUTH = 1;
    public static final int EAST = 2;
    public static final int WEST = 3;

    public void northSouthEastWest(String Word)
    {
        int upDownLeftRight = (int)(Math.random() * 3);
        switch(upDownLeftRight){
        case NORTH: fitWordNorth(Word);break;
        case SOUTH: fitWordSouth(Word);break;
        case WEST: fitWordWest(Word);break;
        case EAST:  fitWordEast(Word);break;
        }
    }

You may consider creating another class with properties row,column, p,i and include the instance of this object in your puzzle class.

4
  • It has to be an aray list for the project. What does "break" do? Apr 18, 2012 at 14:30
  • @ThomasMoore List is the interface, ArrayList can be your instance. You should always use the object using an interface. "break" will avoid calling the next case conditions. For example, if the value of upDownLeftRight comes as 1, then with break the outcome will be calling fitWordSouth, and without breaks the outcome will be calling fitWordSouth and all the functions below it. You can test this by writing a simple function with switch and with and without break statements. Apr 18, 2012 at 14:42
  • I have edited my main post's code to show what I have now. Thank you for your time. Apr 18, 2012 at 14:56
  • @ThomasMoore I think you did not get my suggestion. What will be the starting point of your program? When you run a java program, it expects a main method to execute. Your main method should have the functions/objects to be executed. Please add a main method and call the functions in the series of execution. Apr 18, 2012 at 15:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.