Help with number guessing game

Luke Stamper
Ranch Hand
Posts: 64
I am having trouble closing my outer do while loop for the numbers guessing game. Basically, i've generated a random number between 1 and 10. The user guesses what it is. While guessing, they are told if the number is too high or too low. When the get the number right, they are told how many tries it took them to guess correctly. After they guess correctly, they are asked if they want to play again. If they want to play again, the random number should reset and the loop should start over. I am having trouble figuring out how to reset the random number and how to use a Y/N question to initiate the end of or new beginning of the game. I was trying to mess around with an intChoice variable (hit 1 for Yes and 2 for No) but couldn't figure it out.

I am very new to java. Please anyone help?

[Edit: put code between [code] and [/code] ubb code blocks]

Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 11940
210
Luke Stamper wrote:I am having trouble figuring out how to reset the random number

Where are you initializing your variables, and where does your loop start?

Luke Stamper wrote:[I am having trouble figuring out] how to use a Y/N question to initiate the end of or new beginning of the game.

I was trying to mess around with an intChoice variable (hit 1 for Yes and 2 for No) but couldn't figure it out.

It looks to me like the concept of entering 1 to exit is working - you are just not prompting for it.

Where are you having trouble with the idea of a "Y/N" question? Is it the input? Parsing? Comparisons? Something else?

Wouter Oet
Saloon Keeper
Posts: 2700
Small remark: intTries++; is called in each branch of the if-statement. Therefore it could be placed outside of it.

Luke Stamper
Ranch Hand
Posts: 64
I wanted the loop to start with the question of, "welcome to the guessing game, please pick a number between 1 and 10." So when the game was over, the loop would start with that question and a new random number between 1 and 10. I wanted the inner loop to control game play and the outer loop to control whether the user wants to play again or exit. However, I am unsure of what to have for the outer 'do' and the outer 'while'.

I thought I was initializing my variables at the top, but I am not so sure I did that correctly now. I can't get the outer loop to work, I think the problem may be I am not sure I am understanding the concept. Am I missing something that is essential in getting user input to close a loop?

Should I have the random number generated somewhere within the inner loop?

Luke Stamper
Ranch Hand
Posts: 64
Also, thank you Wouter Oet. I've only been working with Java for about a month and I simply don't know little tricks like that. Thanks!

Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 11940
210
Luke Stamper wrote:I wanted the loop to start with the question of, "welcome to the guessing game, please pick a number between 1 and 10." So when the game was over, the loop would start with that question and a new random number between 1 and 10.

Your outer loop starts at line 21.

Your initialization of the target number is at line 18.

See any problems there?

Luke Stamper wrote:I wanted the inner loop to control game play

Which it seems to be doing

Luke Stamper wrote:[I wanted] the outer loop to control whether the user wants to play again or exit. However, I am unsure of what to have for the outer 'do' and the outer 'while'.

Within the outer loop (lines 40 - 42) you seem to be telling the user how many times they attempted to guess the number before they got it right, and then you get a number and if it is "1" you exit.

Is this what you wanted?

(Yes, I know that I am being a little obscure in my answers (and my questions), but it will be worth it if you can use these to help you find the solution yourself)

Luke Stamper
Ranch Hand
Posts: 64

I almost got it! Now I am having an issue with resetting the amount of tries it takes to guess the correct number after you restart the game.

Next up...converting everything to JOptionPane...(ugh)...do you have any words of wisdom in doing this?

Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 11940
210
Luke Stamper wrote:I almost got it! Now I am having an issue with resetting the amount of tries it takes to guess the correct number after you restart the game.

Same set of questions for you - at what line number are you setting / resetting the amount of tries to guess the correct number? At what line number does your outer loop start?

Luke Stamper wrote:Next up...converting everything to JOptionPane...(ugh)...do you have any words of wisdom in doing this?

I would recommend you refactor your program before you look at a GUI. Right now it is one monolithic block of code, where every bit of logic resides in your main method. You might want to consider whether you can break this out into multiple methods, and whether you can get away from having everything run in static code block(s).

Luke Stamper
Ranch Hand
Posts: 64

So i'm trying to use JOptionPane, but I cannot get it to display what's in the loops. Can you provide any advice on that and on how to reset the number of tries?

Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 11940
210
When you try to run this, something appears on your console window. Does that help you see the problem?

Luke Stamper
Ranch Hand
Posts: 64
I got the intTries to reset. Thanks!

Luke Stamper
Ranch Hand
Posts: 64
This is the message I get...

--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at guessingGameLukeStamper.main(guessingGameLukeStamper.java:38)

Process completed.

I'm sorry to say I am not quite sure what this means.

Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 11940
210
First thing is that the source code you have posted above is not the same source code you have on your computer, since the line numbers do not match up.

The very first part of the exception tells you what the problem is: you got a NumberFormatException for input string "". Therefore Java is telling you that "" is not a valid number.

Keep working your way back through the stack trace, and you will eventually find some of your code. In this case it tells you that the problem is at guessingGameLukeStamper.java:38 (line 38 in the code on your computer). In the source code you provided to us, this was line 28:

intGuess = Integer.parseInt(strInput);

The question for you now is: what is the value of strInput? Where do you set it?

Campbell Ritchie
Sheriff
Posts: 50168
79
Andrew Monkhouse wrote: . . . the line numbers do not match up. . . .
They match even less now. The lines were too long, so I added some new lines. At least now you can see the whole code on your screen.

Luke Stamper
Ranch Hand
Posts: 64

It works! Thanks everyone for your help (especially Andrew, you're the man! thank you so much).