• 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

Very new to java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am using Head First Java book.Using textedit and terminal on a mac. This will not compile Any help would be greatly appreciated. Thanks in advance
guessgame.java:1: class GuessGame is public, should be declared in a file named GuessGame.java
public class GuessGame {
^
guessgame.java:59: class Player is public, should be declared in a file named Player.java
public class Player {
^
guessgame.java:66: class GameLauncher is public, should be declared in a file named GameLauncher.java
public class GameLauncher {
^
3 errors
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One java file can only have one public class <ClassName>.
For your case, you basically you need 3 java files.
- GuessGame.java
- Player.java
- GameLauncher.java

So don't put everything into one file.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can remove "public" key words
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch! (Always nice to see more Mac users. )

As already indicated, each .java file can contain at most one top-level class that's declared public. (It is not required to contain any.) If a .java file contains a top-level public file, then the name of the file must match that public class name exactly.

But also note that Java is case-sensitive, so "guessgame" (the name of your file) is not the same as "GuessGame" (the name of the class).

The best approach here is to put each top-level class in its own separate file, named to match the class name. This keeps things nice and neat, so you can easily find a class definition by the name of the file. Although as lei suggested, you could get around this by removing the "public" modifier from the class declarations.
 
Amy Salerno
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I removed the extra public word. Thank you all for your replies
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic