Computer Science 121
Example using a StringTokenizer.

// Application for a clerk in a store. It reads the prices, calculates the tax and displays the total bill.
// The prices are all entered on a single line, separated by 'white space'.
// White space consists of either spaces, tabs or newline characters.

import java.io.*;
import java.text.*;
import java.util.*;

// Main class that declares a class that describes a store clerk and calls the methods.
public class Store6
{
    public static void main (String [] args)
    {
        Clerk storeClerk = new Clerk ();
        storeClerk.readPrices ();
        storeClerk.calculateTax ();
        storeClerk.displayTotal ();
    } // method main
} // class Store

// Class that defines the work of a store clerk.
class Clerk
{
    private double price, sum = 0, tax, total;
    private int numberPrices = 0;
    private String priceList;

    // Method that reads the price of a product from the keyboard.
    public void readPrices ()
    {
        try
        {
            System.out.print ("Enter prices, separated by spaces. ");
            BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
            priceList = stdin.readLine ();
            StringTokenizer prices = new StringTokenizer (priceList);
            while (prices.hasMoreTokens ())
            {
                price = Double.valueOf (prices.nextToken ()).doubleValue ();
                sum = sum + price;
                numberPrices ++;
            }
        } // try block
        catch (IOException e) { System.out.println ("I/O error");}
        catch (NumberFormatException ex) { System.out.println ("Number format error.");}
    } // method getPrice

    // Method that calculates the tax.
    public void calculateTax ()
    {
        final double taxRate = 0.0825;
        tax = sum * taxRate;
    } // method calculateTax

    // Method that calculates and then displays the total bill.
    public void displayTotal ()
    {
        total = tax + sum;
        System.out.println ("The number of items bought is " + numberPrices);
        System.out.println ("The sum is $" + decimals (sum));
        System.out.println ("The tax is $" + decimals (tax));
        System.out.println ("The total bill is $" + decimals (total));
    } // method displayTotal

    // Formats a double for string output with two decimal places.
    public String decimals (double num)
    {
        DecimalFormat decFor = new DecimalFormat ();
        decFor.setMaximumFractionDigits (2);
        decFor.setMinimumFractionDigits (2);
        return decFor.format (num);
    } // method decimals
} // class Clerk