From b4dbd2cfa724476162fa6d35941a5d7cdc9c9524 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 18 Feb 2019 08:10:10 -0500 Subject: Adds all assignments not previously in a git repo --- CS2452/DictionarySearch.java | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 CS2452/DictionarySearch.java (limited to 'CS2452/DictionarySearch.java') diff --git a/CS2452/DictionarySearch.java b/CS2452/DictionarySearch.java new file mode 100644 index 0000000..5d86a91 --- /dev/null +++ b/CS2452/DictionarySearch.java @@ -0,0 +1,91 @@ +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class DictionarySearch +{ + public static String[] dict; + + public static void main(String[] args) + { + BufferedReader ir = new BufferedReader(new InputStreamReader(System.in)); + BufferedReader fr = null; + try + { + fr = new BufferedReader(new FileReader("/home/tje/workspace/Recursion/dictionary.wwf")); + } catch (FileNotFoundException e1) + { + e1.printStackTrace(); + } + + String str = null; + dict = new String[0]; + System.out.println("Dictionary loading..."); + + do + { + try + { + str = fr.readLine(); + } catch (IOException e) + { + e.printStackTrace(); + } + + String[] tmp = new String[dict.length + 1]; + int i; + for (i = 0; i < dict.length; i++ ) + { + tmp[i] = dict[i]; + } + tmp[i] = str; + dict = tmp; + + } while (str != null); + System.out.println("Dictionary loaded."); + + while (true) + { + System.out.print("What word would you like to look up? "); + try + { + System.out.println("The closest word is " + check(ir.readLine().toLowerCase(), 0, dict.length)); + } catch (IOException e) + { + e.printStackTrace(); + } + + + + } + } + + public static String check(String word, int a, int b) + { + if (a == b -1) + { + return dict[a]; + } + int mid = (a+b)/2; + if(word.compareTo(dict[mid]) > 0) + { + return check(word, mid, b); + } + + if (word.compareTo(dict[mid]) < 0) + { + return check(word, a, mid); + } + + if (word.compareTo(dict[mid]) == 0) + { + return dict[mid]; + } + + + return word; + + } +} \ No newline at end of file -- cgit v1.1