   import java.util.Scanner;
/******************************************************** 
  * Sentinel.java is a mini example showing the use of
  * a sentinel value to control loop execution.
  *
  * @author Nancy Harris
  * @version V1 - 10/19/2008
  *******************************************************/
    public class Sentinel
   {
   /* main method runs through the 3 loop types
    *
    * @param args Unused in this application
    */
       public static void main(String args[])
      {  
         final char END_VAL = 'q';
      
         int sum;			// will store sum of a series of numbers
         int num;			// will store each incoming number
         String text;   // will store text values
         Scanner scan;  
      
         scan = new Scanner(System.in);
      // in any accumulator it is important to initialize
      // the accumulator outside of the loop.
      
      // WHILE loop
         sum = 0;
      
      // We must get the condition set up so that we can test the end
      // condition. This is easier in the do loop 
         text = "bogus";  // a value that will cause the loop to execute
      
      // why are we testing for length?
         while (text.length() > 0 && text.toLowerCase().charAt(0) != END_VAL)
         {   
            System.out.print("Enter an integer, q to quit: ");
         
            if (scan.hasNextInt())  //we have something to read in
            {
               num = scan.nextInt();
               sum = sum + num;
					scan.nextLine(); // consume the new line character
            }
            else							// we have something that is text
            {
               text = scan.nextLine();
               if (text.length() > 0 && text.toLowerCase().charAt(0) != END_VAL)
                  System.out.println("You entered " + text + ". This is not valid.");
            }
            System.out.println();
         }	
         System.out.println("While loop sum: " + sum);
      
      // DO While loop
         sum = 0;
      
         text = "bogus";  // a value that will cause the loop to execute
      
         do
         {   
			   System.out.print("Enter an integer, q to quit: ");
         
            if (scan.hasNextInt())  //we have something to read in
            {
               num = scan.nextInt();
               sum = sum + num;
					scan.nextLine(); // consume the new line character
            }
            else							// we have something that is text
            {
               text = scan.nextLine();
               if (text.length() > 0 && text.toLowerCase().charAt(0) != END_VAL)
                  System.out.println("You entered " + text + ". This is not valid.");
            }
            System.out.println();
         
         } while ((text.length() > 0 && text.toLowerCase().charAt(0) != END_VAL));
      
         System.out.println("\nDo While loop sum: " + sum);
     		
      // FOR loop - reinitialize all values
      // This is rather awkward as the update step involves reading in a value
		// NOTE: the update step is missing as a result since it must be in the body
		
		sum = 0;
		
		for (text = "bogus"; text.length() > 0 && text.toLowerCase().charAt(0) != END_VAL;)
      {
      	System.out.print("Enter an integer, q to quit: ");
         
         if (scan.hasNextInt())  //we have something to read in
         {
            num = scan.nextInt();
            sum = sum + num;
				scan.nextLine(); // consume the new line character
         }
         else							// we have something that is text
         {
            text = scan.nextLine();
            if (text.length() > 0 && text.toLowerCase().charAt(0) != END_VAL)
               System.out.println("You entered " + text + ". This is not valid.");
         }
            System.out.println();
      }
      
      System.out.println("\nFor loop sum: " + sum); 
	}
   }
		
		
