• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Guessing Game Question  RSS feed

 
Jon Engle
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening CodeRanch!

I am trying to teach myself some basic Java using the Headfirst book. In Ch 2. there is an example which I have been able to follow (see code below). The problem is this:

$ javac GuessGame.java
GuessGame.java:86: class Player is public, should be declared in a file named Player.java
public class Player {
^
GuessGame.java:95: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher {
^
2 errors


I googled this specific error and found a recommendation to remove the "public" class tags as someone stated there could only be one public class tag per source file. I do not know if this is true. After attempting to remove the public tags I am able to compile but receive an error at runtime. I also found a thread here at JR which discussed the same type of problem but I am still not clear on what the specific remedy was...IE if keeping the multiple sets of public tags is it necessary to create those files and if so is it necessary to compile those files...Also which is a better practice if both options are viable in this scenario...removing public from the other classes or creating additional files?

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



I'm sure whatever I am missing or "not getting" is rather simple but I would really appreciate if someone could point me in the right direction. Thanks for having such a friendly community where one can ask these types of questions

Here is my code thus far:

$ more GuessGame.java





 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As the error messages say, you need three files for your three public classes. They also need to have particular names to match the classes. That is to make it easier for the compiler to find the classes, and most compilers enforce that recommendation with errors.

Additional: I shall (try to) edit your post with code tags, which (if I can save the changes) will make your code look better. But it will also highlight your inconsistent indentation. I suggest you always use 4 spaces at a time.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should compile the three files by compiling the GameLauncher file.
I would prefer to keep the public tags, so you get used to making classes public, but if your three classes are in the unnamed package, you will never notice a difference if you miss out public before class. You must not miss out public before the main method, however.
 
Jon Engle
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning,

Thank you for your quick response. I was able to follow your suggestions in order to make the program successfully work. Also thanks for the tip on indentation, I had no idea on what "proper" code indentation best practices are. What are your recommendations for commenting code (i.e. above, below, 4 spaces out, etc)?

I also wanted to follow up with something that was a little confusing to me which might help someone else out. When taking Campbell's recommendation to make separate files please remember to either remove or remark out the public classes (GameLauncher and Player) from your GuessGame.java class otherwise you will continue to receive the same error even if you have created the new files.

Cheers,

Jon

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome. I don’t agree with everything in this style guide, but I do agree about indenting. I write about text editors frequently; look here, and set up automatic indenting with tab→4 spaces as an option. Then you write code partially backwards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!