• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unexpected errors; where is syntax wrong?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Why do I get these errors on compile:


 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler is complaining because your class syntax is incorrect. If you have a look at the Java Tutorial: Declaring Classes you'll see what is and is not allowed where in a class.

In your case you have statements in the class body which is not allowed. Statements need to be inside a method.
 
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miley Johnson wrote:

Why do I get these errors on compile:



You are probably missing a ending ; or a }, somewhere in the class. Also you should declare your variables and your array before the constructor.
 
Sean Paulson
Ranch Hand
Posts: 94
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:The compiler is complaining because your class syntax is incorrect.



What he said. Unless you want some of those variables to be not be local variables then you can declare them before the constructor. Most of that code does need to be in a method though.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[pedantic]

Technically, lines 14 to 17 just need to be inside a code block, which may not necessarily be a method. For example if you just surround those lines with { }, that would make it an instance initialization block and the code would compile. You can also put that code inside a constructor, which isn't a method.

[/pedantic]
 
Marshal
Posts: 79257
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest it is quicker to use an array initialiser. There is no risk of miscounting the size of the array, nor of leaving any elements uninitialised.
I would also suggest you shouldn't call a class character (surely Character because classes begin with CapitalLetters) to avoid confusion with another class of the same name.
I would also suggest that you shou‍ld get rid of the array and create a Stats class instead.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic