-3
public class PennyPitch {
  
  int total = 0;
  int[][] board = {{1,1,1,1,1}, {1,2,2,2,1}, {1,2,3,2,1}, {1,2,2,2,1}, {1,1,1,1,1}};
  String[][] boardWithP = {{"1", "1", "1", "1", "1"}, {"1", "2", "2", "2", "1"}, {"1", "2", "3", "2", "1"}, {"1", "2", "2", "2", "1"}, {"1", "1", "1", "1", "1"}};
  
  for(int i = 0; i < 4; i = i + 0){
    int x = (int)(Math.random() * 5);
    int y = (int)(Math.random() * 5);
    
    if(boardWithP[x][y] != "P"){
      total += board[x][y];
      boardWithP[x][y] = "P";
      i++;
    }
  }
}

So I keep getting a Syntax error

Syntax error on token ";", { expected after this token

on line 8 and

Syntax error, insert "}" to complete ClassBody

on line to and I was wondering if anyone knew what the problem was. All my brackets seem to match up and there should be a semicolon on line 8 to my knowledge. Any suggestions?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

2 Answers2

0

The issue isn't with the multidimensional arrays, which are fine. The issue is that you can't have code directly under a class - it needs to be in a method, constructor or initializer block. E.g.:

public class PennyPitch {
  
  int total = 0;
  int[][] board = {{1,1,1,1,1}, {1,2,2,2,1}, {1,2,3,2,1}, {1,2,2,2,1}, {1,1,1,1,1}};
  String[][] boardWithP = {{"1", "1", "1", "1", "1"}, {"1", "2", "2", "2", "1"}, {"1", "2", "3", "2", "1"}, {"1", "2", "2", "2", "1"}, {"1", "1", "1", "1", "1"}};
 
  public static void main(String[] args) { // Here!
    for(int i = 0; i < 4; i = i + 0){
      int x = (int)(Math.random() * 5);
      int y = (int)(Math.random() * 5);
    
      if(boardWithP[x][y] != "P"){
        total += board[x][y];
        boardWithP[x][y] = "P";
        i++;
      }
    }
  }
}

Also, as a side note, you shouldn't use the == or != operartors to compare strings, but use the equals method instead. See How do I compare strings in Java? for details.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

You need to put your code inside of a method (function) Like this:

public class PennyPitch {
  public static void main(String[] args) {
     int total = 0;
     int[][] board = {{1,1,1,1,1}, {1,2,2,2,1}, {1,2,3,2,1}, {1,2,2,2,1}, {1,1,1,1,1}};
     String[][] boardWithP = {{"1", "1", "1", "1", "1"}, {"1", "2", "2", "2", "1"}, {"1", "2", 
      "3", "2", "1"}, {"1", "2", "2", "2", "1"}, {"1", "1", "1", "1", "1"}};
  
    for(int i = 0; i < 4; i = i + 0){
       int x = (int)(Math.random() * 5);
       int y = (int)(Math.random() * 5);
    
       if(boardWithP[x][y] != "P"){
          total += board[x][y];
         boardWithP[x][y] = "P";
         i++;
      }
    }
  } 
}