This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes HeadFirst Java 2nd Edition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HeadFirst Java 2nd Edition" Watch "HeadFirst Java 2nd Edition" New topic
Author

HeadFirst Java 2nd Edition

Bob Cook
Greenhorn

Joined: Feb 14, 2009
Posts: 1
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.








Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HeadFirst Java 2nd Edition
 
Similar Threads
GuessGame Errors From Head First Java
Dont understand the guessGame (Head First Java)
Exceptation in thread "main" java.lang.NoSuchMethodError: main
Head First Java Book, Chapter 2
Please Help with Guessing Game (Head First Java)