import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * Read words and definitions from a file and load them to a * binary search tree using a map as interface. Finally print * all the dictionary words and definitions in alphabetical order. */ public class Solution { /** * Gets data from the dictionary file */ private Scanner input = null; /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String args[]) { Solution solution = new Solution(); solution.run(); } /** * Run the solution. This method is called from main() */ public void run() { try { // Create object to read data from a file File file = new File("diccionario.txt"); this.input = new Scanner( file ); // Words and definitions are separated by tabs this.input.useDelimiter("[\\t\\n]+"); // Read the words and definitions into a map BinarySearchTree dictionary = new BinarySearchTree(); // Read the dictionary file while ( this.input.hasNext() ) { // Read a word String word = this.input.next(); // Read a definition, if any String definition = ""; if ( this.input.hasNext() ) { definition = this.input.next(); } // Insert the pair word and definition to our map dictionary.insert(word, definition); } // Ask dictionary to print to standard ouput dictionary.print(System.out); // Close the standard input this.input.close(); } catch ( FileNotFoundException exception ) { // The dictionary file could not be opened System.err.println( exception ); } } }