Java: Printing / returning array from a class

I created a class for a bingo game. I get an error saying "'class' expected". How could I return the values in the array to the main starter? Any other comments would also be helpful. Thank you.

import java.util.Random;
public class Card
{
    Random generator = new Random();

    private final int BOARDMAX = 4; 
    private final int NUMMAX = 59;
    int i, j, m, n;
    private int [][] ArrayBoard = new int[BOARDMAX][BOARDMAX];
    String [][] StrArrayBoard = new String [BOARDMAX][BOARDMAX];

    public void RandomNumGenerator()
    {
        for (i = 0; i<BOARDMAX; i++)
        {
            for (j = 0; j<BOARDMAX; j++)
            {
                ArrayBoard[i][j] = generator.nextInt (NUMMAX+1);
            }
        }

    }   

    public String ShowBoard()
    {
        for (i = 0; i<BOARDMAX; i++)
        {
            for (j = 0; j<BOARDMAX; j++)
            {
                m=i;
                n=j;
                if (j != BOARDMAX)
                    StrArrayBoard[m][n] = ArrayBoard[m][n] + "  ";
                else
                    StrArrayBoard[m][n] = ArrayBoard[m][n] + "  \n";
            }
        }
        return StrArrayBoard[i][j];
    }

    public void ShowMark()
    {
        for (i = 0; i<BOARDMAX; i++)
        {
            for (j = 0; j<BOARDMAX; j++)
            {
                if (CardCheck [i][j] == 1)
                    StrArrayBoard[i][j] = ArrayBoard[i][j] + "* ";
                else
                    StrArrayBoard[i][j] = ArrayBoard[i][j] + "  ";  
                if (j == BOARDMAX)
                    ArrayBoard[i][j] = ArrayBoard[i][j] + "\n";
            }
        }
    }

    public String toString()
    {
        return ArrayBoard[][];
    }
}

Answers


With toString() you need to return a String object but actually you try to return an int[][]. The same is true for ShowBoard, you try to return an array of Stringarrays which is not a compatible type.

Here's the fix:

public String ShowBoard() {
  // your code to populate StrArrayBoard

  StringBuilder boardBuilder = new StringBuilder();
  for (String[] row:StrArrayBoard)
    for (String cell:row)
      sb.append(cell);
  return boardBuilder.toString();
}

public String toString() {
  return ShowBoard();
}

I suggest to refactor the code and rename methods and fields:

ShowBoard()  -->  getBoardAsString()
ArrayBoard   -->  arrayBoard
StrArrayBoard --> strArrayBoard

And there's no need to declare StrArrayBoard as a field (class member) just because you only need it inside the ShowBoard method. Declare it there as a local variable.


Adding to the bugs others have pointed:

You have if (CardCheck [i][j] == 1), but the array CardCheck is not declared anywhere.

You have ArrayBoard[i][j] = ArrayBoard[i][j] + "\n"; but ArrayBoard is an int array, you cannot add a string "\n" to it's member.


The compiler will complain because of the error on your code:

public String toString()
{
    return ArrayBoard[][];
}

It can't convert int[][] (which is your ArrayBoard) to String. My suggestion is that you populate all values stored in StrArrayBoard in a StringBuffer and return the StringBuffer.toString() in the toString() method.

The toString() method requires a String.

Hope this helps.


    public String toString()
    {
        return ArrayBoard[][];
    }

This method expects to return a String but you are returning a 2D Integer array, what you need is a String. the toString() method returns a string representation of the object, so in this case, what you can do is to use a StringBuilder to build the string representation of the array and then, use the .toString() of the StringBuilder to return the string representing the 2D Array.

Also, as noted by Alois Cochard, you variable naming does not follow convention. Variable names in Java use a camel case notation.


I for one don't really understand your question but I've got a couple of comments.

  • The class variables i and j should be local variables in each method.
  • Your naming convention is nonstandard, seems like a more C# convention. Start variable and method names with a lower case.
  • CardCheck isn't defined anywhere. I presume it is meant to indicate if a number on a square has been checked, in which case it should be a boolean and not an int.
  • toString doesnt return a string. You can use Arrays.toString to help you.
  • Similarily, ShowBoard just returns one element of an array, you probably wanted to show the entire board there.

For your toString and ShowBoard methods you probably want to use a StringBuilder to build up the string representation.

        StringBuilder builder = new StringBuilder();
        for (int i=0; i<BOARDMAX; i++) {
            for (int j=0; j<BOARDMAX; j++) {
                builder.append(StrArrayBoard[i][j]);
            }
            builder.append('\n');
        }
        return builder.toString();

Here's a version of your class that compiles (and I changed some field names and modifiers to adhere to standard conventions). Try this:

public class Card{

    private final Random generator = new Random();

    private static final int BOARDMAX = 4;
    private static final int NUMMAX = 59;
    int i, j, m, n;
    private final int[][] arrayBoard = new int[BOARDMAX][BOARDMAX];
    private final String[][] strArrayBoard = new String[BOARDMAX][BOARDMAX];

    // do something here please
    private int[][] CardCheck;

    public void RandomNumGenerator(){
        for(i = 0; i < BOARDMAX; i++){
            for(j = 0; j < BOARDMAX; j++){
                arrayBoard[i][j] = generator.nextInt(NUMMAX + 1);
            }
        }

    }

    public String ShowBoard(){
        for(i = 0; i < BOARDMAX; i++){
            for(j = 0; j < BOARDMAX; j++){
                m = i;
                n = j;
                if(j != BOARDMAX){
                    strArrayBoard[m][n] = arrayBoard[m][n] + "  ";
                } else{
                    strArrayBoard[m][n] = arrayBoard[m][n] + "  \n";
                }
            }
        }
        return strArrayBoard[i][j];
    }

    public void ShowMark(){
        for(i = 0; i < BOARDMAX; i++){
            for(j = 0; j < BOARDMAX; j++){
                if(CardCheck[i][j] == 1){
                    strArrayBoard[i][j] = arrayBoard[i][j] + "* ";
                } else{
                    strArrayBoard[i][j] = arrayBoard[i][j] + "  ";
                }
                if(j == BOARDMAX){
                    // this is probably what you mean:
                    strArrayBoard[i][j] = arrayBoard[i][j] + "\n";
                }
            }
        }
    }

    @Override
    public String toString(){
        // this is probably what you mean:
        return Arrays.deepToString(strArrayBoard);
    }
}

Need Your Help

INSERT INTO WHERE NOT EXISTS

php mysql pdo

I'm trying to insert group names into my database. Since they are usergroups I only want to insert them once, so if I accidentally insert them again it should refuse it because It already exists. I'm

MongoDB with Drupal 7

mongodb drupal-7

I want to use MongoDB with Drupal 7 for the speedup of performance. How can we use a MongoDB in my Drupal website for any particular module. My Drupal site is a social networking site and I want to...