aboutsummaryrefslogtreecommitdiff
path: root/CS2452/DictionarySearch.java
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-02-18 08:10:10 -0500
committerTucker Evans <tuckerevans24@gmail.com>2019-02-18 08:10:10 -0500
commitb4dbd2cfa724476162fa6d35941a5d7cdc9c9524 (patch)
tree431af0b75efa29dfa3bab2868a78ab0eb29173c7 /CS2452/DictionarySearch.java
parente8b1808eaf87a49e4c34ebbfb66854baa627418c (diff)
Adds all assignments not previously in a git repo
Diffstat (limited to 'CS2452/DictionarySearch.java')
-rw-r--r--CS2452/DictionarySearch.java91
1 files changed, 91 insertions, 0 deletions
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