// Helene Martin, CSE 142 // Prompts the user for several words and reports the total number of letters typed. // Demonstrates the use of a sentinel. // This version also reports the longest word typed. import java.util.*; public class WordSum2 { public static final String SENTINEL = "exit"; public static void main(String[] args) { Scanner console = new Scanner(System.in); int letters = 0; // pull out the first prompt ('post') // so word is initialized to something the user // typed in and should be added to the letter count System.out.print("Type a word (or \"" + SENTINEL + "\" to exit): "); String word = console.next(); // int max = 0; -- only need the String! String longest = ""; while (!word.equals(SENTINEL)) { // order of statements needs to be swapped when first 'post' is taken out letters += word.length(); if (word.length() > longest.length()) { // max = word.length(); longest = word; } System.out.print("Type a word (or \"" + SENTINEL + "\" to exit): "); word = console.next(); } System.out.println("Total letters typed: " + letters); System.out.println("Length of lon gest word: " + longest.length() + " (" + longest + ")"); } }