/******************************************************************* * Fiddle plays with Strings. * Reads in strings from the user until end-of-file then prints them * in lists of small, medium, and large. Removes the word "the" from * a clone of the small list. * Uses the Bag implemented as a linked list. * @author Beth Katz * @version March 2008 *******************************************************************/ import java.util.Scanner; public class Fiddle { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); String s; BagLL small = new BagLL( ); BagLL medium = new BagLL( ); BagLL large = new BagLL( ); System.out.println("Enter Strings." + " Finish with return and control-D."); System.out.print("Next? "); while (stdin.hasNext( )) { s = stdin.next( ); if (s.length() < 5) { addStringToBag(small, s, "small"); } else if (s.length( ) < 9) { addStringToBag(medium, s, "medium"); } else { addStringToBag(large, s, "large"); } System.out.print("Next? "); } System.out.println( ); // print input results System.out.println("small list is " + small); System.out.println("medium list is " + medium); System.out.println("large list is " + large); BagLL smaller = small.clone( ); System.out.println("cloned small list is " + smaller); removeWords("the", smaller); System.out.println("cloned small list is now " + smaller); System.out.println("small list is still " + small); } // adds string to given bag with name label public static void addStringToBag(BagLL bag, String str, String label) { bag.add(str); System.out.println("\"" + str + "\" inserted into " + label); } // removes all instances of word from bag public static void removeWords(String word, BagLL bag) { int count = 0; while (bag.remove(word)) { count++; } System.out.println("\"" + word + "\" removed " + count + " times."); } } //the cow jumped gracefully over the harvest moon laughing all the way