How Do You Troubleshoot the Java Compilation Problem “error expected … illegal start of type”?

Problem scenario
When trying to compile a Java program, you get this output:

ContInt.java:15: error: expected
System.out.println("test");
^
ContInt.java:15: error: illegal start of type
System.out.println("test");
^

What do you do?

Solution
Create a class for the System.out.println statement to be in.

Here is code that would cause the error:

import java.util.Scanner;

public class TicTacToe {

public static void main(String[] args) {
   System.out.println("Please enter text, then press enter: ");
   Scanner x = new Scanner(System.in);
   String var1 = x.next();
   System.out.println(var1);
   }

class board {
   System.out.println("test");
  }

}

Here is code that would not cause the error:

import java.util.Scanner;

public class TicTacToe {

public static void main(String[] args) {
   System.out.println("Please enter text, then press enter: ");
   Scanner x = new Scanner(System.in);
   String var1 = x.next();
   System.out.println(var1);
   }

class board {
   board() {
   System.out.println("test");
   }
  }

}

Leave a comment

Your email address will not be published. Required fields are marked *