aethyrr
aethyrr

Reputation: 21

using an array in a toString method

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?

Upvotes: 0

Views: 1772

Answers (3)

Loris Securo
Loris Securo

Reputation: 7638

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;  
    }
}

Upvotes: 0

Ousmane D.
Ousmane D.

Reputation: 56469

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.

Upvotes: 0

VHS
VHS

Reputation: 10184

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;
}

}

Upvotes: 2

Related Questions