Guest User

Untitled

a guest
Dec 16th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6.  
  7. public class WriteToFile {
  8.  
  9. BinarySearchTree bst;
  10. SimilarWords sw;
  11. PrintWriter pw = null;
  12. File f = null;
  13.  
  14. public WriteToFile(BinarySearchTree bst, SimilarWords sw) {
  15. this.bst = bst;
  16. this.sw = sw;
  17. fileWriter();
  18. }
  19.  
  20.  
  21. public void fileWriter() {
  22.  
  23. /**
  24. * creates a new file named "utskrift.txt" if it does not exist.
  25. */
  26.  
  27. f = new File("utskrift.txt");
  28. if(!f.exists()){
  29. try {
  30. f.createNewFile();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. System.out.println("New file \"utskrift.txt\" has been created to the current directory");
  35. }
  36.  
  37. try {
  38. pw = new PrintWriter(f);
  39. } catch (FileNotFoundException fnfe) {
  40. System.err.printf("Could not open '%s':%n%s%n", "utskrift.txt", fnfe.getMessage());
  41. System.exit(2);
  42. }
  43.  
  44. /**
  45. * calls on the tree to get out the statistics for the tree, and prints them out in the file
  46. */
  47.  
  48. pw.println("The depth of the tree: ");
  49. pw.println(bst.heightOfBinaryTree());
  50. pw.println();
  51. pw.println("The average Depth of all the nodes: ");
  52. pw.println(bst.averageDepth());
  53. pw.println();
  54. pw.println("The first word in the dictionary:");
  55. pw.println(bst.findMin());
  56. pw.println();
  57. pw.println("The last word in the dictionary: ");
  58. pw.println(bst.findMax());
  59. pw.println();
  60. pw.println("How many nodes there are for each depth of the tree: ");
  61.  
  62. int[] depth = bst.eachDepth();
  63. for(int i = 0; i < depth.length ; i++){
  64. pw.println(i + " : " + depth[i]);
  65. }
  66.  
  67.  
  68.  
  69. pw.println();
  70. searchWord("etterfølger");
  71. pw.println();
  72. searchWord("etterfolger");
  73. pw.println();
  74. searchWord("etterfølgern");
  75. pw.println();
  76. searchWord("tterfølger");
  77.  
  78. pw.close();
  79. }
  80. /**
  81. * checks if the words are in the dictionary, and if they are not, it checks if there are words that resembles the words.
  82. */
  83. public void searchWord(String input){
  84. pw.println("Searching for the word : " + input);
  85. if(!bst.find(input)){
  86.  
  87. String[] one = sw.findSimilar(input);
  88. for(String x: one){
  89. if (bst.find(x)){
  90. pw.println("found a similar word: " + x);
  91. }
  92. }
  93. }else{
  94. pw.println(input +" was found");
  95. }
  96. }
  97. }
Add Comment
Please, Sign In to add comment