-5

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.

  • 4
    Somewhere you're using a String where you should be using an array. If you post a [mcve] someone can probably tell you where. – khelwood Mar 30 '19 at 09:44
  • "a" is a string. lets see your whole switch statement – Patrick Parker Mar 30 '19 at 09:45
  • Most compilers (especially within an IDE) will tell you the line and column number where the error occurs. If you want us to help, you need to show us enough code to diagnose the problem. – dave Mar 30 '19 at 09:47
  • Just guessing, but could it be you want your `break` statements, outside of your `if` blocks, so that each `case` is properly separated from the next? – dave Mar 30 '19 at 09:48
  • `b[]` is a _integer_ array and `a[]` is an _String_ array.Can't I update the subscript of an **string array** with a **string**. – Shobha singh Mar 30 '19 at 09:50
  • note-I want the fall through to happen. – Shobha singh Mar 30 '19 at 10:03
  • Your class has *fields* `String[][] a=...` and `int[] b=..` but your method declared as `public void printer(int a,String b)` defines their own `int a,String b` variables for holding arguments passed when method will be used like `printer(1,"foo")`. Problem is that inside method when you are using `a` or `b` you are referring to passed argument. I am not sure if your method actually need them. If not then change your method to `public void printer()`. If you need them then either rename each of those variables to be unique, or use `this.a` to refer to field, and `a` to refer to parameter. – Pshemo Mar 30 '19 at 10:27

1 Answers1

0

The problem is that you have "int[] b" defined outside the function. and also String b for the 'printer' method.

So inside the printer method, b is a String however you have tried to assigned as an array within the switch case.

Just change the followings , then it would be fine.

Change 1 : public void printer(int a,**String str**)
Change 2 : switch(**str**)

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158