/* TestTokenizer1.java CIS 160 David Klick 2005-09-09 Demonstration of StringTokenizer to evaluate a series of numbers typed in. This program also demonstrates a priming read, repetition, a sentinel value, parsing strings into numbers, the use of an accumulator and exception handling. */ import java.util.Scanner; import java.util.StringTokenizer; public class TestTokenizer1 { public static void main(String[] args) { double number; double total = 0.0; String delims = " "; Scanner kbd = new Scanner(System.in); System.out.print("Enter a series of numbers (\"QUIT\" to exit): "); String s = kbd.nextLine(); while (!s.equalsIgnoreCase("QUIT")) { StringTokenizer tok = new StringTokenizer(s, delims); while (tok.hasMoreTokens()) { try { number = Double.parseDouble(tok.nextToken()); } catch (NumberFormatException e) { number = 0.0; } total += number; } System.out.print("Enter more numbers (\"QUIT\" to exit): "); s = kbd.nextLine(); } System.out.println("The total is: " + total); } }