Computer Science Example
Using a StringTokenizer

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

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

// Class that defines the work of a store clerk.
class Clerk
{
     private int numberItems = 0;
     private double total = 0;

     /* Method that reads the prices of a list of items from the keyboard.  It uses a StringTokenizer to get the individual prices and add them up.*/
     public void readPrices ()
     {
          double price;
          String priceList;
          try
          {
               BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
               System.out.print ("Enter prices, separated by spaces. ");
               priceList = stdin.readLine ();
               StringTokenizer priceString = new StringTokenizer (priceList);
               while (priceString.hasMoreTokens ())
               {
                    price = Double.parseDouble (priceString.nextToken ());
                    total = total + price;
                    numberItems ++;
               }
          } // try block
          catch (NumberFormatException ex) { System.out.println ("Number format error.");}
          catch (IOException e) {System.out.println ("IO Exception");}
     } // method getPrice

     // Method that calculates the tax and returns the value.
     private double calculateTax (double sum)
     {
          final double taxRate = 0.0825;
          double tax = sum * taxRate;
          return tax;
     } // method calculateTax
 
     // Method that calculates and then displays the total bill.
     public void displayTotal ()
     {
          double tax = calculateTax (total);
          double totalBill = total + tax;
          System.out.println ("The number of items bought is " + numberItems);
          System.out.println ("The total is $" + decimals (total));
          System.out.println ("The tax is $" + decimals (tax));
          System.out.println ("The total bill is $" + decimals (totalBill));
     } // method displayTotal
 
     // Formats a double for string output with two decimal places.
     public static String decimals (double num)
     {
          DecimalFormat decFor = new DecimalFormat ();
          decFor.setMaximumFractionDigits (2);
          decFor.setMinimumFractionDigits (2);
          return decFor.format (num);
     } // decimals
 
} // class Clerk