   import java.util.Scanner;
/******************************************************** 
  * Flag.java is a mini example showing the use of
  * a flag to control loop execution.
  *
  * @author Nancy Harris
  * @version V1 - 10/19/2008
  *******************************************************/
    public class Flag
   {
   /* main method runs through the loop 
	 *
    * @param args Unused in this application
    */
       public static void main(String args[])
      {  
         final int HI_VAL = 10;
			final int LO_VAL = 1;
			
			boolean goodValue;
			int number;
			      
         Scanner scan;  
      
         scan = new Scanner(System.in);

			// validation.  We want to validate our input such that 
			// it must be an int and it must be between lo and hi values
			// inclusively.
			// we set the flag to false, until we read something in 
			// that is a good value.
			
			number = 0;         // must initialize since we use it after the structure
			goodValue = false;  // start here.  We don't yet have a goodValue
			
			while (!goodValue)
			{
				System.out.print("Enter a number between " + LO_VAL + " and " + HI_VAL + ":");
				
				if (scan.hasNextInt())
				{
					number = scan.nextInt();  // partway...we know its an int
					if (number >= LO_VAL && number < HI_VAL)
						goodValue = true;			// now we know its in range
						
					else								// we know that its out of range
					{
						System.out.println("Your value " + number + " is not in the right range.  Try again.");
					}
					scan.nextLine();  // consume new line in prep for next read
				}
				else
					System.out.println("Your value " + scan.nextLine() + " is not an integer.  Try again.");
			} // end while
			
			System.out.println("Congratulations! You entered a correct number. " + number);			 
		}
	}	
					