001package algs35; 002import stdlib.*; 003/* *********************************************************************** 004 * Compilation: javac BlackFilter.java 005 * Execution: java BlackFilter blacklist.txt < input.txt 006 * Dependencies: SET In.java StdIn.java StdOut.java 007 * Data files: http://algs4.cs.princeton.edu/35applications/tinyTale.txt 008 * http://algs4.cs.princeton.edu/35applications/list.txt 009 * 010 * Read in a blacklist of words from a file. Then read in a list of 011 * words from standard input and print out all those words that 012 * are not in the first file. 013 * 014 * % more tinyTale.txt 015 * it was the best of times it was the worst of times 016 * it was the age of wisdom it was the age of foolishness 017 * it was the epoch of belief it was the epoch of incredulity 018 * it was the season of light it was the season of darkness 019 * it was the spring of hope it was the winter of despair 020 * 021 * % more list.txt 022 * was it the of 023 * 024 * % java BlackFilter list.txt < tinyTale.txt 025 * best times worst times 026 * age wisdom age foolishness 027 * epoch belief epoch incredulity 028 * season light season darkness 029 * spring hope winter despair 030 * 031 *************************************************************************/ 032 033public class BlackFilter { 034 public static void main(String[] args) { 035 args = new String[] { "data/list.txt" }; 036 StdIn.fromFile ("data/tinyTale.txt"); 037 038 SET<String> set = new SET<>(); 039 040 // read in strings and add to set 041 In in = new In(args[0]); 042 while (!in.isEmpty()) { 043 String word = in.readString(); 044 set.add(word); 045 } 046 047 // read in string from standard input, printing out all exceptions 048 while (!StdIn.isEmpty()) { 049 String word = StdIn.readString(); 050 if (!set.contains(word)) 051 StdOut.println(word); 052 } 053 } 054}