I am very new to java. Please anyone help?
[Edit: put code between [code] and [/code] ubb code blocks]
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?
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 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.
OK, let's start with that as a desirable.
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)
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?
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).
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?
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
I'm sorry to say I am not quite sure what this means.
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?
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.
Andrew Monkhouse wrote: . . . the line numbers do not match up. . . .
It works! Thanks everyone for your help (especially Andrew, you're the man! thank you so much).