import java.net.URL; import java.util.Set; import java.util.Scanner; import java.io.IOException; public class IndexTest { private static Index index = new Index(); public static void main(String[] searchWords){ try { // first fill the index up from some sample pages indexPage(new URL("http://www.gustavus.edu/+max/")); indexPage(new URL("http://www.gustavus.edu/+max/courses/S2006/MCS-287/syllabus.html")); indexPage(new URL("http://java.sun.com/")); // now search the index for pages with the given words for(int i = 0; i < searchWords.length; i++){ if(i > 0){ System.out.println(); // blank line between searches } System.out.println("Hits for " + searchWords[i] + ":"); for(URL u : index.get(searchWords[i])){ System.out.println(u); } } } catch(Exception e){ e.printStackTrace(); System.exit(1); } } private static void indexPage(URL u) throws IOException{ Scanner s = new Scanner(u.openStream()); s.useDelimiter("[^a-zA-Z0-9]+"); while(s.hasNext()){ index.put(s.next(), u); } } }