Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Scanner;
- public class Diction {
- public static final Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
- @Override
- public int compare(String h1, String h2) {
- return h1.compareToIgnoreCase(h2);
- }
- };
- private static void createFile(String fileName) {
- try {
- File file = new File(fileName);
- file.createNewFile();
- } catch (IOException e) {
- System.out.println("Exception Occurred:");
- e.printStackTrace();
- }
- }
- private static HashMap<String, String> readFileToDictionary(String fileName) {
- HashMap<String, String> dictionary = new HashMap<String, String>();
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(fileName));
- String line;
- while ((line = br.readLine()) != null) {
- String[] stuff = line.trim().split(":");
- dictionary.put(stuff[0], stuff[1]);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return dictionary;
- }
- private static void writeToFile(ArrayList<String> fc, String fileName) {
- try {
- FileWriter fileWriter =
- new FileWriter(fileName);
- BufferedWriter bufferedWriter =
- new BufferedWriter(fileWriter);
- for(int i = 0; i < fc.size(); i++){
- bufferedWriter.write(fc.get(i));
- if(i != fc.size()-1)
- bufferedWriter.newLine();
- }
- bufferedWriter.close();
- }
- catch(IOException ex) {
- System.out.println(
- "Error writing to file '"
- + fileName + "'");
- }
- }
- public static void exitPorgram(HashMap<String, String> dictionary, String filename){
- ArrayList<String> words = convertToArray(dictionary);
- ArrayList<String> formatted = new ArrayList<String>();
- for(int i = 0 ; i < words.size(); i ++){
- formatted.add(words.get(i) + ":" + dictionary.get(words.get(i)));
- }
- writeToFile(formatted, filename);
- System.exit(0);
- }
- public static ArrayList<String> convertToArray(HashMap<String, String> dictionary){
- ArrayList<String> dic = new ArrayList<String>();
- for(String s:dictionary.keySet()){
- dic.add(s);
- }
- dic.sort(ALPHABETICAL_ORDER);
- return dic;
- }
- public static void searchWord(Scanner scn, HashMap<String, String> dictionary){
- System.out.println("What word would you like to search for?");
- String word = scn.nextLine();
- if (dictionary.containsKey(word)) {
- System.out.println(word + ": " + dictionary.get(word));
- } else {
- System.out.println(
- "That word doesn't exist in the dictionary. Please type the definition you would like to give it.");
- dictionary.put(word, scn.nextLine());
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String fileName = ("src\\" + "dictionary" + ".txt");
- createFile(fileName);
- Scanner scn = new Scanner(System.in);
- HashMap<String, String> dictionary = readFileToDictionary(fileName);
- while (true) {
- System.out.println("Type a number to perform a corresponding action:");
- System.out.println("- 1: Search for a word in the dictionary.");
- System.out.println("- 2: Print the entire dictionary.");
- System.out.println("- 3: Exit program and save dictionary.");
- switch(scn.nextLine()){
- case "1":
- searchWord(scn, dictionary);
- break;
- case "2":
- ArrayList<String> words = convertToArray(dictionary);
- for(String s:words){
- System.out.println(s + ": " + dictionary.get(s));
- }
- break;
- case "3":
- exitPorgram(dictionary, fileName);
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement