File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes GuessGame errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GuessGame errors" Watch "GuessGame errors" New topic
Author

GuessGame errors

Brad Cantrell
Greenhorn

Joined: Aug 27, 2005
Posts: 22
Im trying to compile the GuessGame program in Head First Java and Im getting two identical errors that say:

GuessGame.java:59: 'class' or 'interface' expected
GuessGame.java:68: 'class' or 'interface' expected

It should be noted these lines are in blank space after the code in my GuessGame file. Ive got Player and GameLauncher in there own .java files. Here is the code in my GuessGame file:
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
My guess is that there are unmatched closing curly braces. Overall, the formatting of your code looks pretty good. However, it is easier to tell how many closing braces you need if you unindent them one level. For example, you currently have:

I suggest that you change this to

Similarly for all curly braces. In particular, all the curly braces at the very end are at the same level. If you unindent each one an extra level than the one above, it will help indicate which block of code each one closes off. This will help make sure you have the correct number of closing braces since the last one in the file should be unindented all the way.

If this doesn't fix the problem, please indicate exactly which blank lines the error messages refer to.

Layne


Java API Documentation
The Java Tutorial
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I also have some additional comments about your code. First of all, you have declared p1, p2, and p3 as member fields. However, these variables are only used within the method startGame(). I think it would be best to declare these all as local variables instead. One reason is that as instance variables, the Player objects that you create will not be available for garbage collection because the references to them will exist as long as the GuessGame instance exists. On the other hand, as local variables all the objects will be available for garbage collection as soon as the startGame() method ends. In this particular instance, there is probably no significant difference. However, as you learn to write larger programs, memory management may become an issue that you need to think about. It's a good idea to get into the habit of thinking about such things.

Next, you have the following lines of code:

First of all, I would combine these together to declare the variable at the same time you assign it a value. One reason for this is that 0 is a valid guess that the player may enter. If you happen to forget to assign the actual guess or if you modify the code, it will be difficult to tell if a 0 is from the initial value or if it is a valid guess. So I suggest that you combine this into one line:

Second, I don't see where p1number is declared. Is this supposed to be p1.number? Since you have not posted the Player class, I don't know if it has a member field called number. Assuming that there is, it will also need public (or perhaps default or protected) access in order for this to compile. However, public member variables are strongly discouraged because it gives other classes access to implementation details. This breaks encapsulation, which is a basic principle of Object Oriented Programming.

If you are allowed to modify the Player class, I suggest that you change p1.guess() to return an int representing the number that the player guesses. This way you can just do

This is a much cleaner way of implementing this since you don't need to know how the player's guess is stored. Also, it avoids the problem of trying to access p1.number before calling p1.guess().

Finally, you have these two lines of code:

Notice that the second line of code is indented. This is not a typical convention. In fact, all the following lines are indented to the same level. This makes the code a little confusing to read. I suggest that you unindent all of the code until the next closing brace.

Well, I hope you are enjoying learning Java. Please keep coming back with more questions. I'll be more than happy to give feedback (whether you ask for it or not ).

Layne
Brad Cantrell
Greenhorn

Joined: Aug 27, 2005
Posts: 22
Good eye Layne. I cant beleive Im making this many mistakes just copying code out of a book. You were right about the brackets, I forgot an entire while statement. And you were right about the p1.number, I forgot the period and mismatched the numbers. I got the code to compile now, but when I run it I get the following error:

Exception in thread "main" java.lang.NoSuchMethodError: main

What could be wrong now?

I also want to mention that I agree with you about the code redundecy, but since Im just starting out in this book, I really didnt want to take too much time trying to figure out how to optimize code.
thanks
[ August 27, 2005: Message edited by: Brad Cantrell ]
Stephen Boston
Ranch Hand

Joined: Jul 14, 2005
Posts: 165
Hi!

I assume you are running the application like this?
java GuessGame

?

If so, your class does not have a main method.

Try adding a main method.


You'll add this to your class just before the end } of the class.
[ August 27, 2005: Message edited by: Stephen Boston ]

Steve<br /> <br />No matter where you go, there you are.<br /> <br />"My evil self is at the door, and I have no power to stop it."
Brad Cantrell
Greenhorn

Joined: Aug 27, 2005
Posts: 22
Thanks Steve, but it does have a main method defined in the other class file. I should have been more clear, this program is made up of 3 files, GuessGame.java , Player.java and GameLauncher.java. I will list the code for all 3:



and the updated code for GuessGame:

Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
To run it, rather than
java GuessGame
you should type
java GameLauncher
because it is the class that contains the main method.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Brad Cantrell
Greenhorn

Joined: Aug 27, 2005
Posts: 22
Marilyn- I tried that and got the exact same error message

Update: I found out what the problem is, there is no GameLaucher.class file in the folder (I discovered the solution by simply entering my error message in a yahoo search). Now Im trying to compile the GameLaucher.java file put the compiler says it cant read the file. Thats strange, because it is a .java file, and the code looks fine.

Update#2: I GOT IT TO RUN!!!
I simply spelled GameLauncher wrong in the command line.
[ August 28, 2005: Message edited by: Brad Cantrell ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GuessGame errors