Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-08-2010, 07:24 PM   PM User | #1
gnardog
New to the CF scene

 
Join Date: Apr 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gnardog is an unknown quantity at this point
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
gnardog is offline   Reply With Quote
Old 04-08-2010, 07:44 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
gnardog (04-08-2010)
Old 04-08-2010, 08:07 PM   PM User | #3
gnardog
New to the CF scene

 
Join Date: Apr 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
gnardog is an unknown quantity at this point
I really appreciate your input. I'll give it a try and hope it works. Thanks!
gnardog is offline   Reply With Quote
Old 04-09-2010, 03:29 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
np, post back if you have any other questions
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 PM.


Advertisement
Log in to turn off these ads.