7 Replies - 9859 Views - Last Post: 16 March 2012 - 09:04 PM Rate Topic: -----

#1 edd21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-March 12

Trying to print my board methods to output.

Posted 16 March 2012 - 09:05 AM

I am trying to print the board to output after each change. The first change sets the entire board to one symbol. The second change sets the board to alternating symbols. I need the printBoard() method to only print the board, not modify it.

Any help would be greatly appreciated.


import java.util.Scanner;

public class Board 
{
		static int i;				//variable to set rows and column equal to the same
    	static String sym;			//symbol used to set entire array to same symbol
    	static String sym1;			//symbol used with sym2 to alternate and create checkboard
    	static String sym2;			//symbol used with sym1 to create checkerboard
    	static String sym3;			//used in the insertSymbol method
    	static int r;				//used in insertSymbol method. Represents row
    	static int c;				//used in the insertSymbol method. represents column.
    	static Scanner in;			//scanner variable called in
    	static String [][] Board;	//declaring a string array called Board
	
	public void setSize()			//method to set the size of the array
	{
		System.out.println("Please enter a whole number to represent" +
			"the length and width of your board.");
		i=in.nextInt();
		Board= new String[i][i];
	}
	
	public void placeSymbol()		//method to place a symbol in every cell of the array
	{
		System.out.println("Please enter any character and" +
			"this character will fill the board.");
		sym = in.next();
		for(int column = 0; column <i; column++)
			for(int row = 0; row<i; row++)
				Board[row][column]=sym;
	}
	
	public void placeSymbols()		//method to place two symbols alternating in the array
	{
		int test;
		System.out.println("Please enter a Character.");
		sym1=in.next();
		System.out.println("Please enter another character.");
		sym2=in.next();
			for (int row = 0; row < i; row++) 
			{
				for (int col = 0; col < i; col++) 
				{
					
					test = row + col; 
					if (test % 2 == 0) 
					{
						Board[row][col] = sym1; // add character at even array position
					}
					else 
					{
						Board[row][col] = sym2; // add character at odd array position
					}
				}
			}
	}
	public void insertSymbol()		//method to insert a specific symbol into a specific address of the array
	{
		System.out.println("Please enter the row number you"+
			"want to insert at.");
		r=in.nextInt();
		System.out.println("Please enter the column number"+
			"you want to insert at.");
		c=in.nextInt();
		System.out.println("Please enter the character you want"+
			"inserted at that address.");
		sym3=in.next();
		Board[r][c]=sym3;
	}
	
	public void printBoard()
	{
		//Board someBoard=new Board();
		/*String output = "";
		for (int row = 0; row < i; row++) {
            for (int col = 0; col < i; col++) {
                output += sym;
            }
            output += "\n";
        }*/
        //someBoard.Board();
		  System.out.println(Board);
    
	}
	
	 public static void main(String[] args) 
    {
    	//int action;					//variable action used in switch staement
    	in = new Scanner(System.in);
    	boolean done = false;
    	Board someBoard=new Board();
    	
    	System.out.println("Follow the instructions \n");
    	
		while (done!=true)
		{
		someBoard.printBoard();
		someBoard.setSize();
		someBoard.placeSymbol();
		someBoard.printBoard();
		someBoard.placeSymbols();
		someBoard.insertSymbol();
		someBoard.printBoard();
		done = true;
		
    	}
    	
    	
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Trying to print my board methods to output.

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 09:18 AM

So you are declaring a string[][] named Board inside a class named Board? You're not paying for names by the byte. Save yourself and others the confusion and avoid doing this sort of thing. BoardMatrix[][] for example conveys the actual purpose without confusion.

 I need the printBoard() method to only print the board, not modify it.


Your printBoard() method only has one line of active code. What makes you think this is modifying anything?
    public void printBoard()
    {
     System.out.println(Board);   
    }

Was This Post Helpful? 1
  • +
  • -

#3 edd21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-March 12

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 09:22 AM

Good idea about the naming, I will change that.

As you can see in the printBoard method, there are commented out sections that used to print the actual board. I do not want that code in that method. My one line of code doesn't do anything at the moment and this is where I am stuck. I wish to call printMethod which will call printSymbol first and then after getting two more symbols, select from printSymbols.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 09:24 AM

I don't understand your question. Your printBoard() method will just print the Board address
What about
	public void printBoard()
	{
		for(int i = 0; i < Board.length; i++) {
			for(int j = 0; j < Board[i].length; j++) {
				System.out.print(" " + Board[i][j]);
			}
			System.out.println();
		}
        }


you can call it wherever you want
Was This Post Helpful? 1
  • +
  • -

#5 edd21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-March 12

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 09:29 AM

That works initially but afterward I need it to change to the desired output for the next method placeSymbols after it gets two new symbols which places them in alternating positions on the board.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 09:48 AM

Really don't understand your question. Just put a call to printBoard() at the end of your placeSymbol(s)() method.
BTW really not a good idea to have 2 methods only differentiate by a "s" at the end
Was This Post Helpful? 1
  • +
  • -

#7 edd21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-March 12

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 03:14 PM

Here is my improved code. However I do currently have one issue. When I print my alternating symbols method, its prints the board twice. I am not sure why. Earlier I attempted to put that portion of the method in a while loop using a count to try and end it but that did not work out for me. Any more help would be greatly appreciated.

Thanks.

import java.util.Scanner;

public class Board 
{
	static int i;				//variable to set rows and column equal to the same
	static String sym;			//symbol used to set entire array to same symbol
	static String sym1;			//symbol used with sym2 to alternate and create checkboard
	static String sym2;			//symbol used with sym1 to create checkerboard
	static String sym3;			//used in the insertSymbol method
	static int r;				//used in insertSymbol method. Represents row
	static int c;				//used in the insertSymbol method. represents column.
	static Scanner in;			//scanner variable called in
	static String [][] BoardArray;	//declaring a string array called BoardArray
	static int test;
	static int boardCount = 0;

	public void setSize()			//method to set the size of the array
	{
		System.out.println("Please enter a whole number to represent" +
		" the length and width of your board.");
		i=in.nextInt();
		BoardArray= new String[i][i];
	}

	public void placeSymbol()		//method to place a symbol in every cell of the array
	{
		
		System.out.println("Please enter any character and" +
		" this character will fill the board.");
		sym = in.next();
		for(int column = 0; column <i; column++)
			for(int row = 0; row<i; row++)
				BoardArray[row][column]=sym;
	}

	public void alternateSymbols()		//method to place two symbols alternating in the array
	{
		System.out.println("Please enter a Character.");
		sym1=in.next();
		System.out.println("Please enter another character.");
		sym2=in.next();
		
		for (int row = 0; row < i; row++) 
		{
			for (int col = 0; col < i; col++) 
			{
				test = row + col; // adds each position...
				if (test % 2 == 0) 
				{
					BoardArray[row][col] = sym1; // add character at even array position
				}
				else 
				{
					BoardArray[row][col] = sym2; // add character at odd array position
				}
				System.out.print(" " + BoardArray[row][col]); // Prints my alternate output
			}
			System.out.println(" ");
		}
	}
	public void insertSymbol()		//method to insert a specific symbol into a specific address of the array
	{
		System.out.println("Please enter the row number you"+
		" want to insert at.");
		r=in.nextInt();
		r--;
		System.out.println("Please enter the column number"+
		" you want to insert at.");
		c=in.nextInt();
		c--;
		System.out.println("Please enter the character you want"+
		" inserted at that address.");
		sym3=in.next();
		BoardArray[r][c]=sym3;
		      
      
	}

	public void printBoard()
	{
		for(int i = 0; i < BoardArray.length; i++) 
		{
			for(int j = 0; j < BoardArray.length; j++) 
			{
				System.out.print(" " + BoardArray[i][j]);
			}
			System.out.println();
		}
   }

	
	
	
	public static void main(String[] args) 
	{
		in = new Scanner(System.in);
		boolean done = false;
		Board someBoard=new Board();
		
		System.out.println("Follow the instructions \n");
		
		while (done!=true)
		{
			
			someBoard.setSize();			// Sets size of board.
			someBoard.placeSymbol();	// Sets character of board.
			someBoard.printBoard();		// Prints single character board.
			
			someBoard.alternateSymbols(); 	// Places alternating symbols on board.
			
			someBoard.printBoard(); 	// Prints alternating symbol board.
			someBoard.insertSymbol();	// Gets symbol, row, and col to insert into board.
			someBoard.printBoard();		// Prints new selected symbol board.
			done = true;
		}
	}
}




Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Trying to print my board methods to output.

Posted 16 March 2012 - 09:04 PM

This is coming ridiculous if ot pathetic :(

You also print your Board in your xxxAlternateXXXmethod

You have a printBoard() method, use it and STICK to it
When you need to print your board just call it and don't try to RE-write code somewhere else to print it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1