Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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
 
I didn't say it. I'm just telling you what this tiny ad said.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic