// Helene Martin, CSE 142 // Prompts the user for words until they type quit. Reports the // total number of characters typed and the longest word typed. // goal: demonstrate usage of the find the -est algorithm // in this case -- find the longest. import java.util.*; public class WordSum2 { public static final String SENTINEL = "quit"; public static void main(String[] args) { Scanner console = new Scanner(System.in); int letters = 0; String longest = ""; // shortest possible String -- anything else will be longer //int length = 0; not necessary since we can ask longest for its length System.out.print("Type a word or \"" + SENTINEL + "\" to quit: "); String word = console.next(); while (!word.equals(SENTINEL)) { letters = letters + word.length(); if (word.length() > longest.length()) { longest = word; //length = word.length(); } System.out.print("Type a word or \"" + SENTINEL + "\" to quit: "); word = console.next(); } System.out.println("You typed " + letters + " letters"); System.out.println("Longest word " + longest + " (" + longest.length() + " letters)"); } }