This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

HeadFirst Java 2nd Edition  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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: 23832
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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: 64496
225
  • Mark post as helpful
  • send pies
  • 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.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!