This is my array:

``````           pieces[2][1]=new Pawn("Bpn1");
pieces[2][2]=new Pawn("Bpn2");
pieces[2][3]=new Pawn("Bpn3");
pieces[2][4]=new Pawn("Bpn4");
pieces[2][5]=new Pawn("Bpn5");
pieces[2][6]=new Pawn("Bpn6");
pieces[2][7]=new Pawn("Bpn7");
pieces[2][8]=new Pawn("Bpn8");

pieces [1][1]=new Rook("BR1");
pieces [1][2]=new Knight("BN1");
pieces [1][3]=new Bishop("BB1");
pieces [1][4]=new King("BKing");
pieces [1][5]=new Queen("BQueen");;
pieces [1][6]=new Bishop("BB2");
pieces [1][7]=new Knight("BN2");
pieces [1][8]=new Rook("BR2");

pieces[7][1]=new Pawn("Wpn1");
pieces[7][2]=new Pawn("Wpn2");
pieces[7][3]=new Pawn("Bpn3");
pieces[7][4]=new Pawn("Wpn4");
pieces[7][5]=new Pawn("Wpn5");
pieces[7][6]=new Pawn("Wpn6");
pieces[7][7]=new Pawn("Wpn7");
pieces[7][8]=new Pawn("Wpn8");

pieces [8][1]=new Rook("WR1");
pieces [8][2]=new Knight("WN1");
pieces [8][3]=new Bishop("WB1");
pieces [8][4]=new King("WKing");
pieces [8][5]=new Queen("WQueen");;
pieces [8][6]=new Bishop("WB2");
pieces [8][7]=new Knight("WN2");
pieces [8][8]=new Rook("WR2");
``````

i am trying to print it in two loops. but it doesnt work!!!

``````                      String[] drawNull=new String[5];

for(int i=0;i<pieces.length;i++){
for(int j=0;j<pieces[i].length;j++){

if(pieces[2][j]==null){
drawNull[0]="__________________";
drawNull[1]="|                |";
drawNull[2]="|                |";
drawNull[3]="|                |";
drawNull[4]="|_______________ |";

for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);
}
else{

pieces[2][j].print();

}

}

}
``````

print() method is basically located in each chess piece class. here is an example of a pawn class:

``````String[] drawWhitePiece=new String[5];
public void print() {
if(name.charAt(0)=='B'){ // that is the first letter of the name of the piece. if it is black or white.
drawBlackPiece[0]="    _____";
drawBlackPiece[1]="   |_   _|";
drawBlackPiece[2]="   _|  |_ ";
drawBlackPiece[3]=" _| Bpawn |_";
drawBlackPiece[4]="|__________|";
for(int i=0;i<drawBlackPiece.length;i++){
System.out.println(drawBlackPiece[i]);

}
System.out.println( "    "+ name);
}
``````

but it doesnt work!!!

Please show us what your code outputs and what is wrong with it.
Also show what you want the output to look like.

You don't seem to declare a drawBlackPiece array inside your method. Is that the problem?

__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |
__________________
| |
| |
| |
|_______________ |

thats the output. and i want it to look like a chess board

it should look lieke an 8 by 8 board

``http://en.wikipedia.org/wiki/Chess``

The code that i tried

``````for(int i=0;i<pieces.length;i++){
for(int j=0;j<pieces[i].length;j++){

if(pieces[i][j]==null){
drawNull[0]="__________________";
drawNull[1]="|                |";
drawNull[2]="|                |";
drawNull[3]="|                |";
drawNull[4]="|_______________ |";

for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);
}
else{

pieces[i][j].print();

}

}

}``````

Take a peice of paper and write on it what you want printed. Then think how the program will output each line of the printout.
You will have to print one line from each of the eight squares on that row over and over and over until you have completed the squares on that row.
Then do the same for the next row.

i cant come up with an algorithm that will print out objects.

the program doesnt print rows, but prints a single column.

even when i write to print a row like this

``````for (int a = 0; a < pieces.length; a++) {
if (pieces[2][a]!=null){
pieces[2][a].print
}
}
``````

it doesnt let me

here is input

``````for (int a = 1; a < pieces.length; a++) {
pieces [2][a].print();

}
``````

here is output

``````    _____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn1
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn2
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn3
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn4
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn5
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn6
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn7
_____
|_   _|
_|  |_
_| Bpawn |_
|__________|
Bpn8
``````

i tried now to do the following.

``````                  String[] drawNull=new String[5];
for (int a = 1; a < pieces.length; a++) {
if(pieces [a][1]!=null){
pieces [a][1].print();
}
else{
drawNull[0]="__________________";
drawNull[1]="|                |";
drawNull[2]="|                |";
drawNull[3]="|                |";
drawNull[4]="|_______________ |";
for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);
}
}
``````

output:

`````` |-|_|-|_|-|_|
|__________|
|      |
_| name |_
|__________|
_____
|_   _|
_|  |_
_| BPawn |_
|__________|
Bpn1
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
_____
|_   _|
_|  |_
_| Wpawn |_
|__________|
Wpn1
|-|_|-|_|-|_|
|__________|
|      |
_| name |_
|__________|
``````

but i need to print 8 of those columns.
i think i need to add another loop , but i dont know the syntax to make it work

the program doesnt print rows, but prints a single column.

Did you read my last post? Did you understand what I was trying to say about how the print out goes line by line? To get multiple columns you must print a bit of each column before going to the next line.

whats the solution. how do i wrap it up in another loop and print a few columns?

i even tried to put hte whole thing int a method:

which i could print a few times. but that wouldnt work
private static void printcolumn(ChessPiece[][] pieces, int i) {
}

Add another arg to the method that draws/outputs the shapes that tells it which line of its shape to output. All should use print() NOT println() to keep their output on the same line. When the last column has printed its output, println() to a new line

Norm1, i am not sure what you mean..
should i make a method.

print(ChessPiece[][] pieces, int x){
// and put all my thingy in here?
}

then try to manipulate my method to print a few lines seperated by space

``````public class part2{

public static void main(String[] args) {

int[][] board=new int[10][];

for (int i = 0; i < board.length; i++) {
board[i]=new int[i+1];
for (int j = 0; j < board[i].length; j++) {
board[i][j]=(i+1)*(j+1);
}
}
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();
}
}

}``````

i found this code. and it is capable to print a row. but why?

Call the following for each row of the current board row. It defines 6 lines to be printed.

``````// Draw the String for the requested row
public void print( int row) {
drawBlackPiece[0]="    _____";
drawBlackPiece[1]="   |_   _|";
drawBlackPiece[2]="   _|  |_ ";
drawBlackPiece[3]=" _| Bpawn |_";
drawBlackPiece[4]="|__________|";
drawBlackPeice[5] = name;

// print the requested row
System.out.print(drawBlackPiece[row]);
}``````
``````String[] drawNull=new String[5];
String[] row= new String[8];
for (int i = 0; i < row.length; i++) {
row[i]=new String[i];// for some reason it gives me , cannot convert String to string.. why?

for (int a = 1; a < pieces.length; a++) {
if(pieces [a][1]!=null){
pieces [a][1].print();
}
else{
drawNull[0]="__________________";
drawNull[1]="|                |";
drawNull[2]="|                |";
drawNull[3]="|                |";
drawNull[4]="|_______________ |";
for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);
}

}

}``````

could something like that work. i want the output to be stored inside String[]column.

then print it like that.

System.out.println(column[1]+" "column[2]+" "+.....);

what if i did something like this. create an array of object and then try to print that array. would one array hold a whole column?

``````String[] drawNull=new String[5];
Object[] column1= new Object[8];
for (int i = 0; i < column1.length; i++) {
column1[i]=pieces[i][1];

System.out.println(column1[i]);

for (int a = 1; a < pieces.length; a++) {
if(pieces [a][1]!=null){
pieces [a][1].print();
}
else{
drawNull[0]="__________________";
drawNull[1]="|                |";
drawNull[2]="|                |";
drawNull[3]="|                |";
drawNull[4]="|_______________ |";
for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);
}

}

}``````

would one array hold a whole column?

Yes there should be no problem.

``````static void board(ChessPiece[][] pieces, String[] drawNull, int x){
for (int a = 1; a < pieces.length; a++) {
if(pieces [a][x]!=null){
pieces [a][x].print();
}
else{

for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);
}

}

}``````

i created a method for this.
and i called her from the main class.

board(pieces,drawNull,4);

but how can i call each method and arrange it in a row... next to one another.help

Print the lines for each column in a row using print() with a println() at the end to move to the next line.

how do i do it?

how do you use write the code?

Let's say you do this:

System.out.println("a");
System.out.println("b");

That produces:
a
b

If you do this:
System.out.print("a");
System.out.print("b");

That produces:
ab

Does that help?

I thought you were going to put all the lines for each column in an array.
Say theDisplayStrings is a String[8][8] that holds all the lines to be printed

``````for(int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
System.out.print(theDisplayStrings[row][col] + "\t");  // print string in this column and tab
} // end for(col)
System.out.println(); // move to next line
} // for(row)``````

coil, i got it. it does print me something horizontal (the whole thing is horizontal.)

but i established the whole thing. is there a way that i could print it horizontally without changing much of my code.

something along those lines:

``````board(pieces,drawNull,4);
System.out.print("      ");
board(pieces,drawNull,3);``````

a way that i could print it horizontally without changing much of my code.

Perhaps.
Make a small simple program that compiles and executes to work on the technique before you put the logic into your large program.

Norm1, thats what i was trying to do...

i really feel bad about getting rid of my method,.. i wish there was a way

could i use get toString

should i change teh method that i have created. would that help?

Have you tried laying out on a piece of paper line by line what the output should look like and where each segment of each line is to come from?

i can print columns. thats what i want , but with the new method i created..
i cant manipulate it at all. no matter how i try.. i feel that i am stuck

``````}

public void board(ChessPiece[][] pieces, String[] drawNull, int x){
for (int a = 1; a < pieces.length; a++) {
if(pieces [a][x]!=null){
pieces [a][x].print();

System.out.print("_______________________");

}
else{
for(int m=0;m<drawNull.length;m++)
System.out.println(drawNull[m]);

System.out.print("__________________");
}

}

}``````

if i could use some boxing,, may be something like that

ChessInterface chess=new ChessInterface();

string x= chess.board(pieces,drawNull,2);
string x2= chess.board(pieces,drawNull,2);
then, i could do

System.out.print(x)
System.out.print(x2)

but with only calling the method, i can do nothing

could toString help me in any way. may be i should switch the method?

i think what i need to do is may be to form a new multidimentional array..

String[][] printPiece=new String[8][6];

i have got two arrays.
array to print null picture/square[6]
array to print pieces pictrure/square.[6]

so i have 8 rows,
printPiece[1]=Rook[0][1][2][3][4][5][6][7];
printPiece[2]=pawn[0][1][2][3][4][5][6][7];
printPiece[3]=nul[0][1][2][3][4][5][6][7];
[4],,
[5],,,
6,,,
7,,,,,
8,,,,

that 2 dimentional array will print me a column of 8,

but what i think i will be able to is to print the whole thing.

say ,

System.out.print(" "+printPiece[row][column]);
System.out.print(" "+printPiece[row][column]);
System.out.print(" "+printPiece[row][column]);

i am not sure if i am 100% accurate. but thats the direction. what do you think. is it possible?

How does your last posted code address the problem of printing the lines of each column from left to right top to bottom?

i think the whole issue in my previous code is that i made it too rigid.

i need to swap method calling to something else , that would allow me to put the variable/array into the method system.out.print();

so if i will find a way to put my data into an array, i will be able to put that array into a systemout.print() method

i cant think of something else.

i interlocked my whole pieces[][] array into conditions and methods, now i cant get them out and manipulate them..
thats my opinion, but i have studied java for 3 months only

i interlocked my whole pieces[][] array into conditions and methods, now i cant get them out and manipulate them

Yes!
Seeing that is the first step to fixing it. Time to step back and do a little design before doing any more coding. Get that array out, clean up its design, and start with it as a single central shared thing that the methods can use.

okay , i have changed the whole method.

thats what i did. so demonstrating what i did:

``````pieces[2][1]=new Pawn("Bpn1");
pieces[2][2]=new Pawn("Bpn2");
pieces[2][3]=new Pawn("Bpn3");
pieces[2][4]=new Pawn("Bpn4");
pieces[2][5]=new Pawn("Bpn5");
pieces[2][6]=new Pawn("Bpn6");
pieces[2][7]=new Pawn("Bpn7");
pieces[2][8]=new Pawn("Bpn8");

pieces [1][1]=new Rook("BR1");
pieces [1][2]=new Knight("BN1");
pieces [1][3]=new Bishop("BB1");
pieces [1][4]=new King("BKing");
pieces [1][5]=new Queen("BQueen");;
pieces [1][6]=new Bishop("BB2");
pieces [1][7]=new Knight("BN2");
pieces [1][8]=new Rook("BR2");

pieces[7][1]=new Pawn("Wpn1");
pieces[7][2]=new Pawn("Wpn2");
pieces[7][3]=new Pawn("Bpn3");
pieces[7][4]=new Pawn("Wpn4");
pieces[7][5]=new Pawn("Wpn5");
pieces[7][6]=new Pawn("Wpn6");
pieces[7][7]=new Pawn("Wpn7");
pieces[7][8]=new Pawn("Wpn8");

pieces [8][1]=new Rook("WR1");
pieces [8][2]=new Knight("WN1");
pieces [8][3]=new Bishop("WB1");
pieces [8][4]=new King("WKing");
pieces [8][5]=new Queen("WQueen");;
pieces [8][6]=new Bishop("WB2");
pieces [8][7]=new Knight("WN2");
pieces [8][8]=new Rook("WR2");``````

my second array: (absorbed the first array, by the conditions set):

``````String[][] array=new String[9][9];
String nullSquare="      __________________\n      |                | \n      |                |\n      |                |\n      |_______________ |\n";

for (int a = 0; a < pieces.length; a++) {
for (int b = 0; b < pieces[a].length; b++){
if(pieces [a][b]!=null){
array[a][b]=pieces [a][b].print();
}
else{

array[a][b]=nullSquare;

}

}
}``````

print funciton inside each piece class contains a string (it returns the string)

``````public String print() {

if(name.charAt(0)=='B'){
pawn="         ______ \n         |_   _| \n          _|  |_ \n        _| BPawn |_\n       |__________|\n";

}

else{
pawn="         ______ \n         |_   _| \n          _|  |_ \n        _| WPawn |_\n       |__________|\n";

}
return pawn;

}``````

i initiate the array from the main method like this:

``````for(int y=1;y<9;y++){
for(int x=1;x<9;x++){
System.out.print(array[x][y]);

}
System.out.println();
}``````

this is my output

``````|-|_|-|_|-|_|
|__________|
|      |
_| BRook |_
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
|-|_|-|_|-|_|
|__________|
|      |
_| WRook |_
|__________|

|____------|
____|   __|
|_______|
_| BKnight |_
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
|____------|
____|   __|
|_______|
_| WKnight |_
|__________|

|-|
|   |
|     |
| BBishop  |
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| BPawn |_
|__________|
|-|
|   |
|     |
| WBishop  |
|__________|

{(-)([-]) (-)}
|_|-|_|-|_|
{      }
_{BKing }_
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
{(-)([-]) (-)}
|_|-|_|-|_|
{      }
_{WKing }_
|__________|

|-----------|
|          |
|      |
|BQueen|
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
|-----------|
|          |
|      |
|WQueen|
|__________|

|-|
|   |
|     |
| BBishop  |
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
|-|
|   |
|     |
| WBishop  |
|__________|

|____------|
____|   __|
|_______|
_| BKnight |_
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
|____------|
____|   __|
|_______|
_| WKnight |_
|__________|

|-|_|-|_|-|_|
|__________|
|      |
_| BRook |_
|__________|
______
|_   _|
_|  |_
_| BPawn |_
|__________|
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
__________________
|                |
|                |
|                |
|_______________ |
______
|_   _|
_|  |_
_| WPawn |_
|__________|
|-|_|-|_|-|_|
|__________|
|      |
_| WRook |_
|__________|``````

everything is printed in a line, how do i print in rows??!?!?

everything is printed in a line, how do i print in rows??!?!?

Does "in a line" mean that there are NO newline characters on the output so that everything printed is on one line?

If you had read some of the previous posts you would have seen this discussed. Go back and read them.