Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

    Code:
    public static int guess(){
           Scanner scan = new Scanner(System.in);
           	try {
             int guess = scan.nextInt();
           	}
                catch (InputMismatchException err) {
                	System.out.println("Your entry is not an integer between 1 and 100");
                	Guess.guess();
                }
    		return guess;
        }
    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?

    :edit:
    Other than posting in the right forum...sorry :P
    Last edited by ItsMeSean; 10-21-2011 at 01:54 AM. Reason: Wrong Forum

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,111
    Thanks
    75
    Thanked 4,335 Times in 4,301 Posts
    In Java, variables only have scope between the { and } where they are declared.

    Since you code
    Code:
           	try {
                 int guess = scan.nextInt();
           	}
    that variable is not accessible outside the try block.

    The solution is easy (and common!)
    Code:
    public static int guess(){
        Scanner scan = new Scanner(System.in);
        int guess;
        try {
            guess = scan.nextInt();
        }
        catch (InputMismatchException err) {
            System.out.println("Your entry is not an integer between 1 and 100");
            Guess.guess();  // if this is a recursive call, I think it's a bad idea
        }
        return guess;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •