permaculture playing cards*
The moose likes Beginning Java and the fly likes Error in compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error in compiling" Watch "Error in compiling" New topic
Author

Error in compiling

Anita Tennis
Greenhorn

Joined: May 12, 2010
Posts: 2
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
Sheriff

Joined: Sep 28, 2004
Posts: 18541
    
  40


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Anita Tennis
Greenhorn

Joined: May 12, 2010
Posts: 2
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

Joined: Oct 27, 2005
Posts: 19654
    
  18

You might want to UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38039
    
  22
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in compiling
 
Similar Threads
K&B SCJP 1.6 book -> "collections" : error in code ?
Loop Qestion Head First pg 63
head first beersong problem?
Be the compiler excercise in chap 3
Array Out of Bounds Exception