Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Adding a variable to a list, then printing the list

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I've got a simple guessing game with a randomly generated integer.

I'm trying to keep a record of the guesses which the player makes.

I've made an array list, and have attempted to add each guess to it, although I'm only getting the most recent guess in diamond brackets (<>).

 
Bartender
Posts: 2220
47
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you don't add anything to guesses in else statements.

You have too many repeats in your code. If you want to add a guess to the list on each iteration why not adding it before if conditions? That way you would not need to add this line in every branch of execution.
 
Ranch Hand
Posts: 47
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you do not add anything to "guess" unless when the value is guessed.

If you use debugger you probably can see it in step-by-step execution mode.
 
Marshal
Posts: 7181
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, probably you don't want to instantiate the game if the value passed to the constructor is...?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, thanks for the help. I'm definitely going to tidy the code once I've got the code down.

I'm having issues with a "Game Counter". I want to count the number of times a player successfully enters the number.

I thought the code I have would've done this, as it seems reasonably straightforward. Can anyone see where I am overlooking a possible error?

 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is myList.clear() supposed to do?
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myList.clear should wipe the list of entered guesses, which are printed after a correct answer is provided.
 
Liutauras Vilda
Marshal
Posts: 7181
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll repeat myself once again, your Game will suffer from exception, if value to the constructor is passed less or equal to 0. You should make sure it will never happen.
I assume you're not handling this outside Game class.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong with the clear() call. You appear only to clear it is you guess right first time. You have quite a lot of repeated code in lines 28-42 which should be got rid of. Work out which parts are duplicated and consider putting only the unique lines of code should be inside the inner if‑else.

You can use the ?: operator for try/tries.
 
Campbell Ritchie
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't the game over option precede too high and too low? Where are you resetting the counter to 0?
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!