Q:

Java beginner help, cannot resolve a variable

I'm trying to make sure that the variable num is only 5 digits long, but my while and return statements say that num cannot be resolved to a variable. What can be done?
 import java.util.*;
    public class program1
{
  public static void main(String[] args)
  {
    int num;
    num = getnum();
    System.out.print(num);
  }
  public static int getnum()
  {
    Scanner console = new Scanner(System.in);
    do
    {
    System.out.println("Enter a number that has only five digits ");
    int num = console.nextInt(); 
    }
    while (num < 10000 || num > 99999);
    return num;
  }
}

A:

You need to declare num outside of the loop.
int num;
do
{
    System.out.println("Enter a number that has only five digits ");
    num = console.nextInt(); 
} while (num < 10000 || num > 99999);

This is due to variable scoping. It's a bit annoying, but a variable declared in the do portion of a do-while isn't available in the while portion.

