/* Marty Stepp, CSE 142, Autumn 2008 This program prompts the user for words until a particular word "goodbye" is typed, then displays which word was the longest word typed. Example output: Type a word (or "goodbye" to quit): Obama Type a word (or "goodbye" to quit): McCain Type a word (or "goodbye" to quit): Biden Type a word (or "goodbye" to quit): Palin Type a word (or "goodbye" to quit): goodbye The longest word you typed was "McCain" (6 letters) */ import java.util.*; public class LongestWord { public static void main(String[] args) { Scanner console = new Scanner(System.in); String longest = ""; // longest word seen so far // moved one "post" out of loop System.out.print("Type a word (or \"goodbye\" to quit): "); String word = console.next(); while (!word.equals("goodbye")) { if (word.length() > longest.length()) { longest = word; // moved to top of loop } System.out.print("Type a word (or \"goodbye\" to quit): "); word = console.next(); } System.out.println("The longest word you typed was \"" + longest + "\" (" + longest.length() + " letters)"); } }