If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Try Catch in Method

Hi everyone, I'm attempting to create a number guessing game for my object oriented programming class. Myself and my group mates managed to get the game working (albeit probably very sloppily) except for one thing, our try catch.

The issue I'm having is returning the int guess down to the bottom of the method from the try. The obvious way (creating an int guess = 0 before the try catch) wont work because it negates the users next guess after having to go through the catch automatically making it 0. In addition, we must use the separate method to return the users guess.
Any suggestions?

As both you and I said, the result cannot grab the input from inside the try and simply initializing guess without a value will not work. Now, as i stated, I can use int guess = 0; where you have int guess . My issue is in my code for some strange reason if I do that, after the user enters a guess which gets caught in the catch, no matter what they enter directly after, it automatically replaces it with a 0.

The reason I included that return Guess.guess(); is because my professor wants the players turn to continue, without adding to the count, until they enter the proper integer.

For example, in a two player game this is how it would initialize:
1. User 1 enter: d (or any string/float)
2. Output: Error, must be an integer, re-enter (or whatever my output in the catch is)
3. User 1 enter: 4
4. User 2 enter: 90
5. Output: User 1, guess cannot be below 1 (if the user enters 0 or a negative. In this case the guess, for some reason, is given the value 0 and cancels out the value 4 which user 1 enters the second time)
6. Output: User 2, guess is (either too low or too high depending on their number)
Then in 7 and so on users re enter guesses until a winner is chosen.

Believe me, I'm still only a novice at coding but I have tried many different solutions including making the try catch into a try string and then just channging the string to an int before it is returned, but that did not work either. If anyone could even give me a more effective try catch that would work for this sort of thing that would be okay too.

:edit: I would hate to do this, but if you would like (to better assist in the solution process) I can upload the entirety of my program here so you can see exactly what I mean. I've tried using debugger to see exactly where the problem is, but I just can't put my finger on it.

Last edited by ItsMeSean; 10-21-2011 at 04:58 AM.
Reason: Helpfullness