Read Also: [Fixed] Syntax error on token "else", delete this token
As always, first, we will produce the syntax error on token(s), misplaced construct(s) error before moving on to the solution. Let's dive deep into the topic:
Note: This syntax-based compiler error can be produced only in the Eclipse IDE.
[Fixed] Syntax error on token(s), misplaced construct(s)
1. Producing the error using the else statement instead of the else if statement
We can easily produce this error in the Eclipse IDE by using an else statement instead of an else if statement as shown below in the example:
public class SyntaxError { public static void main(String[] args) { String str = "Alive is Awesome"; int length = str.length(); if (str == null) { System.out.println("null value"); }else (length == 0){ System.out.println("zero value"); } else { System.out.println("some value"); } } }
Output:
1.2. Explanation:
The cause of this error is due to using the else statement instead of the else-if statement.
1.3. Solution:
The above compilation error can be resolved by changing the else statement to the else if statement as shown below in the code:
public class SyntaxError { public static void main(String[] args) { String str = "Alive is Awesome"; int length = str.length(); if (str == null) { System.out.println("null value"); }else if(length == 0){ System.out.println("zero value"); } else { System.out.println("some value"); } } }
Output:
some value
2. Producing the error by misplacing the import statements
We can easily produce this error in the Eclipse IDE by using the import statement after the end of the class as shown below in the example:
public class SyntaxError { public static void main(String args[]){} }import java.util.Set;
Output:
2.2. Explanation:
The cause of this error is due to the import statement present after the end of the class. It should be present at the top of the class just after the package statement(if present).
2.3. Solution:
The above compilation error can be resolved by using the import statement at the top of the class as shown below in the code:
import java.util.Set;public class SyntaxError { public static void main(String args[]){} }
3. Producing the error by using casting on the left side of an assignment statement
We can easily produce this error in the Eclipse IDE by using casting on the left side of an assignment statement as shown below in the example:
public class SyntaxError { public static void main(String[] args) { int n = 2; int[] arr = new int[n]; for (int i = 1; i < n; i++) {(int)arr[i] = new Integer(12);} } }
Output:
3.2. Explanation:
The cause of this error is due to using casting on the left side of an assignment statement.
3.3. Solution:
The above compilation error can be resolved by removing the casting from the left side of an assignment statement. You can not use the cast on the left side of an assignment statement, it can be only on the right side as shown below in the code:
public class SyntaxError { public static void main(String[] args) { int n = 2; int[] arr = new int[n]; for (int i = 1; i < n; i++) {arr[i] = (int) new Integer(12);} } }
That's all for today. Please mention in the comments if you have any questions related to the syntax error on token(s), misplaced construct(s) error in Java.