Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unknown Syntax Error  RSS feed

 
Tom Collier
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I've had Java do a weird syntax error as I was going over a few beginner java stuff to get it stuck in my head and remembered properly!
So this was the confusing part for me. The captital letter will be the errors/problems in the code. I left my code comments in just in case!



<-- IT ALSO WANTS A "}" HERE AS WELL.

Also my switch statement seems to be displaying an error although I believe it to be correct :/



Apologies in advance I'm both new to the forum and Java, also a great thanks to everyone who helps, just trying to learn! My only thought could be because their in the same package?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First, when it comes to syntax errors, it means that it is not valid Java (to the compiler). This means that the compiler can get confused. And for these cases, it is often off in the location of the error. So, don't be too concerned regarding the line number -- just think of it a "around" that location.

Anyway, Java requires that statements (that are not declarations) be in initializers, constructors, or methods. You are not allowed to have code just anywhere in the class. So, this is what the compiler is complaining about.

Henry
 
Tim Cooke
Marshal
Posts: 3872
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, what Henry said.

You may find the Oracle "Getting Started" tutorial a good place to start. Or pick up a good book such as Head First Java.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch

I think both are “statements must be inside methods” errors, as Henry has already said. Another thing about errors is that one error may cause lots of other errors, so if you correct the first error, you may apparently solve several for the price of one. Did you get any errors for x? I couldn't see where it was declared.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!