• 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

Error in compiling

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am compiling my code in DOS command prompt and keep getting the following errors. I've attached the code below the errors. Thanks for any help.

C:\Program Files\Java\jdk1.6.0_20\My Work Files\Chapter 3>javac hobbits.java
hobbits.java:5: class, interface, or enum expected
public static void main(String [] args) {
^
hobbits.java:8: class, interface, or enum expected
int z = -1;
^
hobbits.java:10: class, interface, or enum expected
while (z < 2) {
^
hobbits.java:12: class, interface, or enum expected
h[z] = new Hobbits();
^
hobbits.java:13: class, interface, or enum expected
h[z].name = "bilbo";
^
hobbits.java:14: class, interface, or enum expected
if (z == 1) {
^
hobbits.java:16: class, interface, or enum expected
}
^
hobbits.java:19: class, interface, or enum expected
}
^
hobbits.java:21: class, interface, or enum expected
System.out.println("good Hobbit name");
^
hobbits.java:23: class, interface, or enum expected
}
^
10 errors

MY CODE IS AS FOLLOWS:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Method declarations must be within a class. In this case, the main() method is not declared as part of a class.

Henry
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you're compiling hobbits.java instead of Hobbits.java. Don't know if you have two different files there, but be careful to use "javac Hobbits.java" instead.
 
Anita Tennis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry and Christophe,
Thank you very much, I removed the first curly brackets so the method would be within the class and I typed the correct class name in the compile. It compiled and ran fine.
Much appreciated.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to UseCodeTags next time.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:You might want to UseCodeTags next time.

Since you are new, I have added the tags. I usually say you can see how much better your code looks, but in your case it looks worse, because of inconsistent indentation. If you had indented your code correctly, you would have found the mismatched {} easily.

And welcome to JavaRanch
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic