Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Comparator;
  9. import java.util.HashMap;
  10. import java.util.Scanner;
  11.  
  12. public class Diction {
  13.  
  14. public static final Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
  15.  
  16. @Override
  17. public int compare(String h1, String h2) {
  18. return h1.compareToIgnoreCase(h2);
  19. }
  20.  
  21. };
  22.  
  23. private static void createFile(String fileName) {
  24. try {
  25. File file = new File(fileName);
  26.  
  27. file.createNewFile();
  28.  
  29. } catch (IOException e) {
  30. System.out.println("Exception Occurred:");
  31. e.printStackTrace();
  32. }
  33.  
  34. }
  35.  
  36. private static HashMap<String, String> readFileToDictionary(String fileName) {
  37. HashMap<String, String> dictionary = new HashMap<String, String>();
  38.  
  39. BufferedReader br = null;
  40. try {
  41.  
  42. br = new BufferedReader(new FileReader(fileName));
  43.  
  44. String line;
  45.  
  46. while ((line = br.readLine()) != null) {
  47. String[] stuff = line.trim().split(":");
  48. dictionary.put(stuff[0], stuff[1]);
  49. }
  50.  
  51. } catch (IOException e) {
  52.  
  53. e.printStackTrace();
  54.  
  55. }
  56. return dictionary;
  57. }
  58.  
  59. private static void writeToFile(ArrayList<String> fc, String fileName) {
  60. try {
  61.  
  62. FileWriter fileWriter =
  63. new FileWriter(fileName);
  64.  
  65. BufferedWriter bufferedWriter =
  66. new BufferedWriter(fileWriter);
  67.  
  68. for(int i = 0; i < fc.size(); i++){
  69. bufferedWriter.write(fc.get(i));
  70. if(i != fc.size()-1)
  71. bufferedWriter.newLine();
  72. }
  73.  
  74. bufferedWriter.close();
  75. }
  76. catch(IOException ex) {
  77. System.out.println(
  78. "Error writing to file '"
  79. + fileName + "'");
  80.  
  81. }
  82.  
  83. }
  84.  
  85. public static void exitPorgram(HashMap<String, String> dictionary, String filename){
  86.  
  87.  
  88. ArrayList<String> words = convertToArray(dictionary);
  89. ArrayList<String> formatted = new ArrayList<String>();
  90. for(int i = 0 ; i < words.size(); i ++){
  91. formatted.add(words.get(i) + ":" + dictionary.get(words.get(i)));
  92. }
  93. writeToFile(formatted, filename);
  94. System.exit(0);
  95. }
  96.  
  97. public static ArrayList<String> convertToArray(HashMap<String, String> dictionary){
  98. ArrayList<String> dic = new ArrayList<String>();
  99. for(String s:dictionary.keySet()){
  100. dic.add(s);
  101. }
  102. dic.sort(ALPHABETICAL_ORDER);
  103. return dic;
  104. }
  105.  
  106. public static void searchWord(Scanner scn, HashMap<String, String> dictionary){
  107. System.out.println("What word would you like to search for?");
  108. String word = scn.nextLine();
  109. if (dictionary.containsKey(word)) {
  110. System.out.println(word + ": " + dictionary.get(word));
  111. } else {
  112. System.out.println(
  113. "That word doesn't exist in the dictionary. Please type the definition you would like to give it.");
  114. dictionary.put(word, scn.nextLine());
  115. }
  116. }
  117.  
  118. public static void main(String[] args) {
  119. // TODO Auto-generated method stub
  120. String fileName = ("src\\" + "dictionary" + ".txt");
  121. createFile(fileName);
  122. Scanner scn = new Scanner(System.in);
  123. HashMap<String, String> dictionary = readFileToDictionary(fileName);
  124. while (true) {
  125. System.out.println("Type a number to perform a corresponding action:");
  126. System.out.println("- 1: Search for a word in the dictionary.");
  127. System.out.println("- 2: Print the entire dictionary.");
  128. System.out.println("- 3: Exit program and save dictionary.");
  129. switch(scn.nextLine()){
  130. case "1":
  131. searchWord(scn, dictionary);
  132. break;
  133. case "2":
  134. ArrayList<String> words = convertToArray(dictionary);
  135.  
  136. for(String s:words){
  137. System.out.println(s + ": " + dictionary.get(s));
  138. }
  139. break;
  140. case "3":
  141. exitPorgram(dictionary, fileName);
  142. break;
  143. }
  144. }
  145.  
  146. }
  147.  
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement