import java.util.*;
/**
 * A class to demonstrate the while loop in combination
 * with a Scanner object.
 * 
 * Read doubles until the user enters a non-numeric input.
 * Print the final sum after all the numbers have been read.
 */
public class WhileWithScanner 
{
    public static void main(String[] args)
    {    
        Scanner keyboard = new Scanner(System.in);
        double sum = 0;
        double num = 0;

        // tell the user to enter numbers
        System.out.println("Enter numbers to add (type q to quit): ");
        
        /*
         * Pick off numbers from the input and add them to sum
         * until the user enters a non-numeric value
         */ 
        while (keyboard.hasNextDouble())
        {
            num = keyboard.nextDouble();
            sum += num;
        }

        System.out.println("\nFinal sum: " + sum);
    }
}