PDA

View Full Version : Java Char Array Help


crazymob
06-14-2007, 03:12 AM
Hey guys. I was wondering if you could help me with a java assignment I have due soon. The task was to create a static chess board using a multidimensional array and using characters as the peices (K = king, P = Pawn, etc etc).

From what I know, i've done everything right, but when ever I compile it I get an "incompatible types" on each line of the chess[0][1]=" "; etc etc.

import java.applet.Applet;
import java.awt.*;



public class Ex15CHESS extends Applet {

char[][] chess = new char[2][3];


public void init() {

chess[0][0]="R";
chess[0][1]="k";
chess[0][2]="B";
chess[1][0]="K";
chess[1][1]="Q";
chess[1][2]="P";
}
public void paint (Graphics g) {

g.drawString(" "+chess[0][0]+" "+ chess[0][1]+" "+chess[0][2]+" "+chess[1][0]+" "+ chess[1][1]+" "+chess[0][2]+" "+chess[0][1]+" "+ chess[0][0]+" ",20, 20);
g.drawString(" "+chess[1][0]+" "+ chess[1][1]+" "+chess[1][2]+" ",20, 40);
g.drawString(" "+chess[2][0]+" "+ chess[2][1]+" "+chess[2][2]+" ",20, 60);
}
}


Anyway, that's all of it. Any help at all would be appreciated.

Also, could someone point me in the direction of how I would actually use java to draw the chess board itself?

Thanks

Gox
06-14-2007, 05:46 AM
At first glance I believe your issues stem from your use of " ". The double quotation marks specifies a String, while a single quotation mark specifies a character (i.e. 'k').

The reason it won't compile is because your array is of type char but you're trying to assign a string to it using " ". Even thought your String is only one character long java still interprets it as a String and doesn't no how to convert it to a single Character.

Try the following and see if it help

chess[0][0]='R';
chess[0][1]='k';
chess[0][2]='B';
chess[1][0]='K';
chess[1][1]='Q';
chess[1][2]='P';


Gox

crazymob
06-14-2007, 02:40 PM
Hey it worked.

Thanks again!