/* Marty Stepp, CSE 142 This program prompts for words until the word "quit" is typed, then prints the total number of letters that were typed. It is an example of a sentinel loop, which is a subcategory of fencepost problems. */ import java.util.*; // for Scanner public class Sentinel { public static final String SENTINEL = "quit"; public static void main(String[] args) { Scanner console = new Scanner(System.in); // all variable(s) in your while loop test // MUST be declared before/outside the while loop // fencepost problem: pull first post out int count = 0; System.out.print("Type a word (or \"" + SENTINEL + "\" to exit): "); String word = console.next(); while (!word.equals(SENTINEL)) { count += word.length(); // wire // post System.out.print("Type a word (or \"" + SENTINEL + "\" to exit): "); word = console.next(); } System.out.println("You typed " + count + " characters."); } }