• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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:


 
Marshal
Posts: 5950
407
IntelliJ IDE Python TypeScript 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: 17734
302
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: 80636
471
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic