Question

I am writing a tic tac toe game in java. Right now I am currently stuck on the display board method. The board I am trying to create has lines in which the X's and O's will go into. The error I am currently getting is

TicTac2.java:56: error: illegal start of type return result; ^ TicTac2.java:56: error: ';' expected return result;

I will update this code as I go along it with little problems I encounter. I love this site because it helps so much! Anyway here is my code:

    import java.util.*;

public class TicTac2{
   //declare a constant variable
   public enum Cell {E, X, O};  //this is an O, not 0

   public Cell[][] board;

   public static final int SIZE = 3; //size of each row and each column

   public static void main(String[] args) {
       displayBoard a = new displayBoard();
       System.out.println(a);
   }      

   //displayBoard method
   public static void drawLine() {
      for (int i = 0; i <= 4 * SIZE; i++) {
         System.out.print("-");
      }
      System.out.println();
   }
   public static void displayBoard(char[][] board) {
      drawLine();
      for (int i = 0; i < SIZE; i++) {
         for (int j = 0; j < SIZE; j++) {
            SIZE[i][j] = Cell.E;
            System.out.print("| " + board[i][j] + " ");
         }
         System.out.println("|");
         drawLine();
      }
      System.out.println();
   }
   public String toString(){
      String result = "";
      for(Cell[] row : board) {
         for(Cell col : row)
            result += col;
         }
         result =+ "\n";
      }   
      return result;


   }
Was it helpful?

Solution

You are missing a { bracket

for(Cell col : row) { // <--- add this guy
     result += col;
}

Consider learning to use an IDE. An IDE will point out these errors almost immediately. It will also find all other syntax errors.

OTHER TIPS

displayBoard a = new displayBoard();
System.out.println(a);

I'm confused by these lines.

You are defining DisplayBoard as a method that takes in a two-dimensional array of Cells, but in the code above you are treating it like a class.

You don't want to create a displayBoard object (as it isn't a class), you just want to call the methods (and pass in board!).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top