I was trying to build a TIC-TAC-TOE GAME.For that I used a 2-D String array. I declared it as global. I had to update the subscripts at some point of time. But something went wrong and I don't know what. Can someone tell me what went wrong.
String[][] a={{" "," "," |"," "," "," |"," "," "," ",},
{" "," a "," |"," "," b "," |"," "," c "," ",},
{"___","___","__|","___","___","__|","___","___","___"},
{" "," "," |"," "," "," |"," "," "," ",},
{" "," d "," |"," "," e "," |"," "," f "," ",},
{"___","___","__|","___","___","__|","___","___","___"},
{" "," "," |"," "," "," |"," "," "," ",},
{" "," g "," |"," "," h "," |"," "," i "," ",},
{" "," "," |"," "," "," |"," "," "," ",}};
int[] b={1,1,1,1,1,1,1,1,1};
public void printer(int a,String b)
{
String[][] d={{" "," "," |"," "," "," |"," "," "," ",},
{" "," a "," |"," "," b "," |"," "," c "," ",},
{"___","___","__|","___","___","__|","___","___","___"},
{" "," "," |"," "," "," |"," "," "," ",},
{" "," d "," |"," "," e "," |"," "," f "," ",},
{"___","___","__|","___","___","__|","___","___","___"},
{" "," "," |"," "," "," |"," "," "," ",},
{" "," g "," |"," "," h "," |"," "," i "," ",},
{" "," "," |"," "," "," |"," "," "," ",}};
String c=" X ";
if(a==1)
{ c=" X ";
}
if(a==2)
{ c=" O ";}
switch(b)
{
case "a":
if(b[0]==1)
{ a[1][1]=c;
b[0]=(c.compareTo("X")==1)?-1:0;break;}
case "b":
if(b[0]==1){
a[1][4]=c;
b[1]=(c.compareTo("X")==1)?-1:0;break;}
case "c":
if(b[0]==1){
a[1][7]=c;
b[2]=(c.compareTo("X")==1)?-1:0;break;}
case "d":
if(b[0]==1){
a[4][1]=c;
b[3]=(c.compareTo("X")==1)?-1:0;break;
}
case "e":
if(b[0]==1)
{a[4][4]=c;
b[4]=(c.compareTo("X")==1)?-1:0;break;
}
case "f":
if(b[0]==1){
a[4][7]=c;
b[5]=(c.compareTo("X")==1)?-1:0;break;
}
case "g":
if(b[0]==1){
a[7][1]=c;
b[6]=(c.compareTo("X")==1)?-1:0;break;
}
case "h":
if(b[0]==1){
a[7][4]=c;
b[7]=(c.compareTo("X")==1)?-1:0;
break;}
case "i":
if(b[0]==1){
a[7][7]=c;
b[8]=(c.compareTo("X")==1)?-1:0;break;
}
The compiler keeps giving the error as array expected,but java.lang.String found
.I have provided a bigger code so can someone look into the matter?
Is it allowed in java to update the subscript of a string array with a string? Have I gone wrong by using the string in switch-case statement?
(I don't know much about object oriented programming. I am new in this field).
In case of the String array
a[7][7]=some string value
the error is array required but integer found
but in case of an integer array
b[4]=some integer value
the error is array required but string found
I am not getting this.