A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
Arduino in Action
this week in the
General Computing
forum!
A special promo:
Enter your blog post or vote on a blogger to be featured in an upcoming Journal
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Trying to print my board to output.
Edward Strife
Greenhorn
Joined: Mar 15, 2012
Posts: 14
posted
Mar 15, 2012 21:02:59
0
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; } } }
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
posted
Mar 19, 2012 08:43:19
0
The method printBoard() should print the output value rather than the object Board.
System.out.println(output);
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4905
7
I like...
posted
Mar 19, 2012 08:47:31
0
John Jai wrote:
The method printBoard() should print the output value rather than the object Board.
Actually, there's nothing particularly wrong with doing it that way...
@Edward: ...providing you implement a
toString()
method for your Board.
I'm puzzled. Why did you comment out that loop? It looks more like what you want to do.
Winston
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
posted
Mar 19, 2012 08:55:26
0
Agree... But he also try to create a new instance of Board in the printMethod(). So thought he might understand printing output better
I agree. Here's the link:
http://aspose.com/file-tools
subject: Trying to print my board to output.
Similar Threads
while loop and try-catch
print invalid selction not working
need help with calculation in my spreadsheet array
Switch Statement
array out of bounds
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter