• 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

HeadFirst Java 2nd Edition

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am just starting out learning Java using this text book. I finished reviewing chapter 2 and have run the GuessGame in Eclipse as written in the book. I am trying to make a change so that within the player object I can store the number of times a player guesses a specific number (5 for example). I then want to have this printout at the end of the game to say "player 1 guessed 5 x times), player 2 guessed 5 x times, etc....

I made the following changes to the code and thought I am getting no errors this is not working as intended so I am definitely missing something here. Below is the code in the player and guess game classes with my changes. Any suggestions to point me in the right direction would be appreciated.








 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I made the following changes to the code and thought I am getting no errors this is not working as intended so I am definitely missing something here.



It would probably help if you tell us what you consider "working as intended" or what do you consider "missing something".

But here's a hint...



Where is the p1guessFive variable set? And what is it set to?

Henry
 
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!

You've defined a counter in your Player class as an int variable called "guessFive." That looks fine.

But in your GuessGame class, you define String variables for each player (like "p1guessFive") and set these to null. These have no relation to the int counters in your Player instances, nor do they ever get assigned a value other than null. Do you see why?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch
I don't have that book (I ought to buy a copy) but I have seen that guessing game many times: here on the beginner's forum. A search for "GuessGame" will probably find several previous discussions, and you can see how others handled their problems.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic