Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- public class WriteToFile {
- BinarySearchTree bst;
- SimilarWords sw;
- PrintWriter pw = null;
- File f = null;
- public WriteToFile(BinarySearchTree bst, SimilarWords sw) {
- this.bst = bst;
- this.sw = sw;
- fileWriter();
- }
- public void fileWriter() {
- /**
- * creates a new file named "utskrift.txt" if it does not exist.
- */
- f = new File("utskrift.txt");
- if(!f.exists()){
- try {
- f.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("New file \"utskrift.txt\" has been created to the current directory");
- }
- try {
- pw = new PrintWriter(f);
- } catch (FileNotFoundException fnfe) {
- System.err.printf("Could not open '%s':%n%s%n", "utskrift.txt", fnfe.getMessage());
- System.exit(2);
- }
- /**
- * calls on the tree to get out the statistics for the tree, and prints them out in the file
- */
- pw.println("The depth of the tree: ");
- pw.println(bst.heightOfBinaryTree());
- pw.println();
- pw.println("The average Depth of all the nodes: ");
- pw.println(bst.averageDepth());
- pw.println();
- pw.println("The first word in the dictionary:");
- pw.println(bst.findMin());
- pw.println();
- pw.println("The last word in the dictionary: ");
- pw.println(bst.findMax());
- pw.println();
- pw.println("How many nodes there are for each depth of the tree: ");
- int[] depth = bst.eachDepth();
- for(int i = 0; i < depth.length ; i++){
- pw.println(i + " : " + depth[i]);
- }
- pw.println();
- searchWord("etterfølger");
- pw.println();
- searchWord("etterfolger");
- pw.println();
- searchWord("etterfølgern");
- pw.println();
- searchWord("tterfølger");
- pw.close();
- }
- /**
- * checks if the words are in the dictionary, and if they are not, it checks if there are words that resembles the words.
- */
- public void searchWord(String input){
- pw.println("Searching for the word : " + input);
- if(!bst.find(input)){
- String[] one = sw.findSimilar(input);
- for(String x: one){
- if (bst.find(x)){
- pw.println("found a similar word: " + x);
- }
- }
- }else{
- pw.println(input +" was found");
- }
- }
- }
Add Comment
Please, Sign In to add comment