• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in compiling

 
Anita Tennis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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:
 
Henry Wong
author
Marshal
Pie
Posts: 21493
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to UseCodeTags next time.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic