• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head first java - chapter 5 SimpleDotCom game  RSS feed

 
Noel McCarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am very new to Java and have been working throught the book Head First Java. In Chapter 5 it goes through how to set up a simple dotcom game. I have tried to do this in eclipse, but I am getting one error with a red line under the public class GameHelper, when I hover the mouse over this error the following comment is seen
Te public type game helper must be defined in its own file



Here is the code as i complied it



When i try to Run this code, the following error is returned




Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The public type GameHelper must be defined in its own file

at simpledotcom.GameHelper.<init>(SimpleDotCom.java:4)
at simpledotcom.SimpleDotCom.main(SimpleDotCom.java:73)



Please can someone tell me when I have gone wrong?
Thanks,
 
Deepak Rao
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noel,

You have 2 classes and both are public in the same file. Only one should be public.
Remove the public from GameHelper class.
Run the SimpleDotCom class.

Thanks
Deepak
 
Noel McCarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it works!

thank you!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Noel, can you please UseCodeTags next time? Not only will it preserve indentation, it also automatically takes care of syntax highlighting, so you don't have to do it manually.

And welcome to the Ranch!
 
Noel McCarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew there must have been a simpler way...
Yes I will find out how to use it and thanks for the welcome
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Rao wrote: . . . Remove the public from GameHelper class. . . .
Disagree. you should put the two classes in different files.

I have changed your quote tags to code; don’t use coloured text in code tags. Also you ought to use spaces for indentation. I see you are using Eclipse; you should be able to set an option to convert your tabs to (4) spaces automatically.
 
Noel McCarthy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Deepak Rao wrote: . . . Remove the public from GameHelper class. . . .
Disagree. You should put the two classes in different files.

I have changed your quote tags to code; don’t use coloured text in code tags. Also you ought to use spaces for indentation. I see you are using Eclipse; you should be able to set an option to convert your tabs to (4) spaces automatically.


Thanks once again,

I now have two class objects in my simpledotcom package.
The SimpleDotCom package and the other the GameHelper package.

there are no errors and it works in the compiler, it only lets me enter one guess though and gives the following printout in the console


enter a number 1
hit
you took 1 guesses

and it doesnt give me the option to enter another guess, therefore there is something wrong with my loop?

this is a great resource I have been pouring over this problem for a few days, it got resolved a hell of alot quicker here



 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is controlling the loop to get more inputs?

as you are working in Eclipse now is the perfect time to learn debugging put a break point inside your loop and find out where it all goes wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!