• 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

Head first java - chapter 5 SimpleDotCom game

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 36
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And it works!

thank you!
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic