import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Scanner; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.util.PDFTextStripper; public class Searching { public static void main(String[] args) throws IOException { Scanner input = new Scanner(System.in); PrintStream output = System.out; // convert pdf to text File file = new File("jls8.pdf"); PDDocument document = PDDocument.load(file); PDFTextStripper stripper = new PDFTextStripper(); stripper.setSortByPosition(true); FileWriter fileOutput = new FileWriter("jls8.txt"); stripper.writeText(document, fileOutput); document.close(); fileOutput.close(); // store text in list file = new File("jls8.txt"); Scanner fileInput = new Scanner(file); List words = new ArrayList(); while (fileInput.hasNext()) { String word = fileInput.next(); words.add(word); } fileInput.close(); //output.println(words.size()); output.print("Word to be searched: "); String search = input.next(); long start = System.nanoTime(); boolean found = words.contains(search); long stop = System.nanoTime(); output.println(stop - start); start = System.nanoTime(); found = false; Iterator iterator = words.iterator(); while (iterator.hasNext() && !found) { found = search.equals(iterator.next()); } stop = System.nanoTime(); output.println(stop - start); start = System.nanoTime(); Collections.sort(words); stop = System.nanoTime(); output.println(stop - start); start = System.nanoTime(); int index = Collections.binarySearch(words, search); stop = System.nanoTime(); output.println(stop - start); found = index > 0; } }