Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Placing a Symbol in a Two-Dimensional Array

1. ## Placing a Symbol in a Two-Dimensional Array

Hey all,
I've hit a snag with a question and am hoping for a little guidance. In the question I am supposed to create a grid and have a user inputted character to fill up the space of the grid.
ex. 4X4 with \$
\$\$\$\$
\$\$\$\$
\$\$\$\$
\$\$\$\$

Now, so far I've started out and think that I may be veering down the wrong path. I've created my methods and am not sure that the placeSymbol one is right. My code so far is:
Code:
```
import java.util.*;
public class Board
{
// variables
int x;
int y;
String symbol1;
String[][] twoDArray;

// default constructor
public Board()
{
x = 0;
y = 0;
symbol1 = "";
String[][] twoDArray = new String[100][100];
}
// constructor
public Board(int newRow, int newCol, String newSymbol1)
{
x = newRow;
y = newCol;
symbol1 = newSymbol1;
String[][] twoDArray = new String[x][y];
}

public int placeSymbol()
{
String[][] twoDArray = new String[x][y];
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
{
twoDArray[i][j] = symbol1;
}
}
return 0;
}

public void printBoard(String[][] twoDArray)
{
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
{
System.out.println();
}
System.out.println("" + twoDArray[x][y]);
}
System.out.println();
}

public static void main(String[] args)
{
Board twoDArray;
twoDArray = new Board(8, 8, M);
twoDArray.printBoard();
}
}```
My driver at the end isn't what the final product will be. I just quickly wrote it up to try and print out my board. Needless to say, there is an error there. It doesn't recognize the symbol 'M' in this case. This is why I know there are problems somewhere in my code but don't know where. I would be most thankful for any guidance.

Thanks

• The symbol problem is simple, M just hasn't been defined anywhere.
If your intent is to use the character M, then wrap it in double quotes since the definition is for a string.
Variable handling and method chaining is incorrect, declaring a String[][] inside a method gives it local scope and will be destroyed when the method completes. The consructors should be chained for simplicity, and a call to placeSymbol is missing. PrintBoard also needs correction from the looks of it, assuming it has a matrix type layout, println should only be called in the outer loop, and the inner should just need the character print. If you want to be lazy, String[][] isn't necessary at all.

• ## Users who have thanked Fou-Lu for this post:

gnardog (04-08-2010)

• I really appreciate your input. I'll give it a try and hope it works. Thanks!

• np, post back if you have any other questions

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•