View Single Post
Old 10-21-2011, 03:45 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote