• Register
1 vote
85 views

Problem:

I've got a simple code of java. I am being a failure to compile my program. It throws back an unknown error says, missing '}' or illegal start of expression. Here is my code snippet

public class MyClass {
  public static void main(String[] args) {
    count();  
  public static int count() {
    return 0;
  }
}

Any idea? An advance thanks for your solution.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote
 
Best answer

Solution:

This a compile-time error. The java compiler javac read your program from top to bottom and left to right once you hit the compile button. It throws back the illegal start of expression error once it finds something inappropriate at the start of an expression.

Let’s figure the problems in your code step by step. First of all, you haven’t closed the brace of your main method. As soon as you close the brace first problem is resolved. Now, coming to the second problem, you can’t add another public method inside your main method in java. You can only have public methods outside of your main method.

Always leave a print() function inside your main method to understand whether your program is running or not! The whole program could have been like this:

public class MyClass {
  public static void main(String[] args) {
    count();
    System.out.println("Program is running!");
  }
  public static int count() {
      return 0;
  }
}

Keep exploring. Thanks.

13 9 6
94,240 points
0 votes

This error is a compile time error when the compiler encounters an inappropriate statement in the source code. This error can be encountered in various scenarios . Illegal start of expression can be encountered in various scenarios. The following are the most common errors:

  • Missing an opening or closing curly bracket for a code block
  • Declaring a function inside another function
  • Using an access specifier with a function's local variable
  • String Character Without Double Quote

To debug this error, try looking at the lines preceding the error message for missing brackets, curly braces or semicolons and check the syntax.

2
140 points

Related questions

0 votes
1 answer 9 views
9 views
When I'm building the project, it's good, but impossible to run it. What am I doing wrong?
asked Dec 15, 2020 TeamScript 13.5k points
0 votes
2 answers 68 views
68 views
Problem : Facing bellow Spring boot error unable to start embeddedwebapplicationcontext due to missing embeddedservletcontainerfactory bean.
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
1 answer 9 views
9 views
Problem: I had asked a question about this earlier, but it didn't get answered right and led nowhere. So I've clarified few details on the problem and I would really like to hear your ideas on how could I fix this or what should I try. I have Java 1.6.0.12 installed on my Linux server and the code below runs just perfectly.
asked Feb 14 Ali 5.7k points
0 votes
1 answer 19 views
19 views
how could I fix this or what should I try?
asked Dec 28, 2020 TeamScript 13.5k points
0 votes
2 answers 285 views
285 views
Problem : Today I installed Java 1.6.0.26 on my server user and when I try to run my application, I get the following exception. java.security.invalidkeyexception illegal key size or default parameters
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 11 views
11 views
Can anyone help me to find out the issue? Where is the problem with the server?
asked Feb 12 TeamScript 13.5k points
0 votes
1 answer 9 views
0 votes
1 answer 8 views
8 views
Does anyone know what I can do to fix this?
asked Feb 9 TeamScript 13.5k points