0

I have been working on this project for a while now and I still can't figure out the first part of it. I am trying to make a grid of letters to make a word search, but I can't seem to figure out how to print it. So far I have:

public wordsearch(int row, int col, String letters){
    if (letters.length()== row*col){
        gridletters= letters;
        gridrow=row;
        gridcol=col;
        letters= letters.toUpperCase();

        String array[][]= new String[row][col];

        int character=0;
        for(int rowNum =0; rowNum<row; rowNum++){
            for(int colNum=0; colNum<col; colNum++){
                array[rowNum][colNum] = letters.charAt(character) + "";
                character++;
            }
        }
    }
}

and then I have to print it using a toString method, but I can't access the array in the toString.

enpublic String toString(){
    String grid= "";
    for(int index=0; index<gridrow; index++){
        for(int index2=0; index2<gridcol; index2++){
            grid+= array[index][index2]+ " ";
        }
        grid+= "\n";
    }
    return grid;
}

someone help please?

3 Answers 3

2

You can't access a local variable declared inside one method body in another (unless you call the second method from the first and pass the variable as an argument). You must declare it at a class level.

public class YourClass {

private String array[][];

public wordsearch(int row, int col, String letters){
    if (letters.length()== row*col){
    gridletters= letters;
    gridrow=row;
    gridcol=col;
    letters= letters.toUpperCase();

    array= new String[row][col];

    int character=0;
    for(int rowNum =0; rowNum<row; rowNum++){
        for(int colNum=0; colNum<col; colNum++){
            array[rowNum][colNum] = letters.charAt(character) + "";
            character++;
        }
    }}}

public String toString(){
    String grid= "";
    for(int index=0; index<gridrow; index++){
        for(int index2=0; index2<gridcol; index2++){
            grid+= array[index][index2]+ " ";
        }
        grid+= "\n";
    }
    return grid;
}

}
Sign up to request clarification or add additional context in comments.

Comments

0

and then I have to print it using a toString method, but I can't access the array in the toString.

Somewhere on top of your class declare an array like below:

String globalArray[][];

before the closing brace of your wordSearch method do something like this:

globalArray = array; // point it to the local array

then within your toString() method you can access the globalArray.

Comments

0

array should be an instance variable rather than a local variable. This way it would be accessible from the other methods in your class.

You can find more info about the difference between instance and local variables here: https://stackoverflow.com/a/2088370/6245535

For example:

public class WordSearch {

    private String array[][];  // declare as instance variable

    public WordSearch (int row, int col, String letters) {
        if (letters.length() == row * col) {
            gridletters = letters;
            gridrow = row;
            gridcol = col;
            letters = letters.toUpperCase();

            array = new String[row][col];  // initialize the instance variable

            int character = 0;
            for (int rowNum = 0; rowNum < row; rowNum++) {
                for (int colNum = 0; colNum < col; colNum++) {
                    array[rowNum][colNum] = letters.charAt(character) + "";
                    character++;
                }
            }
        }
    }

    @Override
    public String toString() {
        String grid= "";
        for(int index = 0; index < gridrow; index++) {
            for(int index2 = 0; index2 < gridcol; index2++) {
                grid += array[index][index2]+ " ";
            }
            grid += "\n";
        }
        return grid;  
    }
}

Comments

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.