-1

Feel stupid asked like the exact same question yesterday but today with a different situations can do the same thing.

single[z][i] = (board[i].split("?!^"));  

This line gives me an error: incompatible types required: String found: String[] 1 error I know it because its a 2D array and split gives it own string output but dont know how to fix. Here is all my code:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int player;
    String board[] = new String[8];
    String single [] [] = new String [8] [8];

    //single[0][0] = "hi";

    //If player is 1, I'm the first player.
    //If player is 2, I'm the second player.
    player = in.nextInt();

    //Read the board now. The board is a 8x8 array filled with 1 or 0.

      for(int i = 0; i < 8; i++) {
          if(in.hasNext()){
              board[i]= in.next();

         }
      }
  for(int z = 0; z < 8; z++){
       for(int i = 0; i < 8; i++) {
           single[z][i] = (board[i].split("?!^"));   

    }
  }for(int z = 0; z < 7; z++)
       for(int i = 0; i < 7; i++) {
           if((single[z][i].equals(1)) && (single[z][i+1].equals(1)) &&(single[z+1][i].equals(1))){
               System.out.print(z + ""+ i);
           }
       }


    nextMove(player,board);

   }

   }
JT attack
  • 77
  • 1
  • 1
  • 8

1 Answers1

0

Assuming that the split is expected to return an array of Strings representing the desired 8 elements, you should do splits only in the outer loop, and then iterate over the results of split in the inner loop or in the array copy method:

for(int z = 0; z < 8; z++){
    String[] tokens = board[z].split("?!^");
    // Should have at least 8 tokens here.
    // Copy the first 8 into single[z]
    single[z] = new String[8];
    System.arraycopy(tokens, 0, single[z], 0, 8);
}

If you know that each split returns precisely 8 items, you could do it in an even simpler way:

for(int z = 0; z < 8; z++){
    single[z] = board[z].split("?!^");
}
for(int z = 0; z < 8; z++){
    for(int i = 0 ; i < 8 ; i++) {
        System.out.print("single["+z+"]["+i+"]=");
        System.out.print(single[z][i]);
    }
}
Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
  • Thank you can you explain how this works and how i would implement it in my code i tried and i think i did it wrong – JT attack Jun 30 '15 at 00:17
  • @JTattack To get 2D 8x8 array of strings you need to split 8 strings into 8 pieces each. The loop is for the first dimension; the inner loop is replaced by `arraycopy` If you know the result of `split` has exactly 8 items, you could simplify this further (see edit). – Sergey Kalinichenko Jun 30 '15 at 00:44
  • ok thanks that works but i need both i and z bc there the row and column part of a graph so i need both but it then gives me incompatible parts. Hope this makes sence – JT attack Jun 30 '15 at 00:56
  • @JTattack `split("?!^")` already returns a 1D array, so `i` dimension is already there, you don't need to do anything about it while splitting. Add a pair of nested loops after this one loop, and print elements that you've got with both `z` and `i` to see what is going on. – Sergey Kalinichenko Jun 30 '15 at 00:59
  • I can do that but I need all the points saved before hand. If I do that after the loop is done the data would be forgotten correct? – JT attack Jun 30 '15 at 01:02
  • @JTattack Why, the data you store in `single[][]` is there to stay, it's not going away. – Sergey Kalinichenko Jun 30 '15 at 01:08
  • I'm sorry I'm having a lot of trouble understanding this. In your edit I only see z how do I add i into the mix? – JT attack Jun 30 '15 at 01:12
  • Z is saved to single but i is not and I'm not sure how. My last comment was a little confusing. – JT attack Jun 30 '15 at 01:23