Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial Commit

We have a working CLI, and WordHash is clean.

Frame should probably be mergd into MainGUI.
  • Loading branch information...
commit 95ad2192d049de6fe62168972cf6068d36322580 1 parent 2d81950
@Havvy authored
Showing with 242 additions and 0 deletions.
  1. +70 −0 Frame.java
  2. +126 −0 MainCLI.java
  3. +16 −0 MainGUI.java
  4. +30 −0 WordHash.java
View
70 Frame.java
@@ -0,0 +1,70 @@
+package assignment8;
+
+import java.awt.BorderLayout;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+
+@SuppressWarnings("serial")
+public class Frame extends JFrame implements KeyListener {
+ private final JTextArea input = new JTextArea();
+ private final JLabel labFindWord = new JLabel("Find Word: ");
+ private final JTextField findWord = new JTextField();
+ private final JTextArea output = new JTextArea();
+
+ private String priorInput = "";
+ private WordHash words;
+
+ public Frame () {
+ super("Word Finder");
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ setResizable(false);
+
+ Container window = getContentPane();
+ window.setMinimumSize(new Dimension(600, 1200));
+
+ input.setPreferredSize(new Dimension(600, 300));
+ window.add(input, BorderLayout.NORTH);
+
+ window.add(labFindWord, BorderLayout.WEST);
+
+ findWord.setPreferredSize(new Dimension(590 - labFindWord.getWidth(), 12));
+ window.add(findWord, BorderLayout.EAST);
+
+ output.setPreferredSize(new Dimension(600, 300));
+ output.setEnabled(false);
+ window.add(output, BorderLayout.SOUTH);
+
+ pack();
+ setVisible(true);
+ }
+
+ @Override
+ public void keyPressed(KeyEvent e) {
+ // Unused.
+ }
+
+ @Override
+ public void keyReleased(KeyEvent evt) {
+ Document in = input.getDocument();
+ try {
+ if (in.getText(0, in.getLength()).equals(priorInput)) {
+ priorInput = in.getText(0, in.getLength());
+ output.setText(priorInput);
+
+ words = new WordHash(priorInput);
+ }
+ } catch (BadLocationException e) {
+ System.exit(1);
+ }
+ }
+
+ @Override
+ public void keyTyped(KeyEvent e) {
+ // Unused.
+ }
+
+}
View
126 MainCLI.java
@@ -0,0 +1,126 @@
+package assignment8;
+
+import java.io.*;
+import java.util.Set;
+
+public class MainCLI {
+
+ static public WordHash wordHash;
+ public static String inputString;
+ public static String[] words;
+
+ public static void main(String[] args) throws IOException {
+ wordHash = new WordHash(readstr());
+
+ boolean mainrepeat = true;
+ while (mainrepeat) {
+ System.out.println("*************************************************************************");
+ System.out.println("Hello! Please choose an option (represented by the corresponding number):");
+ System.out.println("(1) Print out the string.");
+ System.out.println("(2) Search for a word in the string");
+ System.out.println("(0) exit the program.");
+ mainrepeat = usrIn();
+ }
+ }
+
+ static public boolean usrIn() throws IOException{
+ String[] printStrings = inputString.split(" ");
+ String iputstr;
+ Set<Integer> indices = null;
+ int chosenvalue = -1;
+ chosenvalue = readval(0, 2);
+
+ switch (chosenvalue) {
+ case 0:
+ System.out.println("Goodbye! Have a nice day! :D");
+ return false;
+ case 1:
+ System.out.println("*************************************************************************");
+ System.out.println("Here is your string:");
+ System.out.println(inputString);
+ return true;
+ case 2:
+ System.out.println("*************************************************************************");
+ System.out.println("What word would you like to search for?:");
+ iputstr = readstr();
+ indices = wordHash.get(iputstr);
+ if (indices == null){
+ System.out.println("The word is not in the document!");
+ } else {
+ System.out.println("Here are your values:");
+ System.out.println("--------------------------------");
+ for (Integer ix : indices) {
+ System.out.println("");
+ System.out.println("****At word " + ix + "*****");
+ printexcript(ix, printStrings);
+ }
+ System.out.println("--------------------------------");
+ }
+ return true;
+ default:
+ System.out.println("*************ERROR IN USRIN METHOD*************");
+ return true;
+ }
+ }
+
+ static public String readstr(){
+ String foundstr = null;
+ BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
+ boolean uix = true;
+ while (uix) {
+ try {
+ foundstr = input.readLine();
+ uix = false;
+ if (foundstr == null) {
+ System.out.println("Please enter a word.");
+ uix = true;
+ }
+ } catch (IOException ex) {
+ System.out.println("Please enter a valid word (ex: 'the', 'cat')");
+ }
+
+ }
+ return foundstr;
+ }
+
+ static public int readval(int check1, int check2) throws IOException {
+ BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
+ boolean uix = true;
+ int status = -1;
+ while (uix){
+ try{ // Reads the user input. If input is not one of the integers, then it re-prompts the user for input
+ status = Integer.parseInt(input.readLine());
+ uix = false;
+ if (status < check1 || status > check2){
+ uix = true;
+ System.out.println("_Incorrect value: please input an integer (between " + check1 + " and " + check2 + ")");
+ System.out.println("");
+ continue;
+ }
+ }catch (NumberFormatException nos){
+ System.out.println("_Incorrect value: please input an integer");
+ uix = true;
+ System.out.println("");
+ }
+
+ }
+ if (status == -1){
+ throw new Error("##########ERROR IN READVAL1 METHOD, QUITTING PROGRAM##########");
+ }
+ return status;
+ }
+
+ static public void printexcript(Integer index, String[] splitstrn){
+ boolean lowerval = ((index - 1) < 0);
+ boolean lower2val = ((index - 2) < 0);
+ boolean upperval = ((index + 1) >= splitstrn.length);
+ boolean upper2val = ((index + 2) >= splitstrn.length);
+
+ System.out.println( "..." + ((!lower2val)?(splitstrn[index - 2] + " "):(".") ) +
+ ((!lowerval)?(splitstrn[index - 1] + " "):(".") ) +
+ splitstrn[index] +
+ ((!upperval)?(" " + splitstrn[index + 1]):(".") ) +
+ ((!upper2val)?(" " + splitstrn[index + 2]):(".")) +
+ "..." );
+ }
+}
View
16 MainGUI.java
@@ -0,0 +1,16 @@
+/**
+ * @author Havvy, Joesph Obanion
+ * @email ryan.havvy@gmail.com, moltow@gmail.com
+ */
+
+package assignment8;
+
+import java.awt.*;
+import javax.swing.*;
+
+public class MainGUI {
+
+ public static void main (String[] argv) {
+ new Frame();
+ }
+}
View
30 WordHash.java
@@ -0,0 +1,30 @@
+package assignment8;
+
+import java.util.*;
+
+@SuppressWarnings("serial")
+public class WordHash extends HashMap<String, Set<Integer>> {
+ public WordHash (String input) {
+ input = input.replaceAll("[^a-zA-Z\\- ]", "").toLowerCase();
+ String[] words = input.split(" |-");
+
+ for(int ix = 0; ix < words.length; ix++){
+ if (words[ix] != null) {
+ put(words[ix], new Integer(ix));
+ }
+ }
+ }
+
+ public void put (String key, Integer value) {
+ Set<Integer> indices = super.get(key);
+ if (indices == null) {
+ // Word not yet in map. We add the word..
+ TreeSet<Integer> set = new TreeSet<Integer>();
+ set.add(value);
+ super.put(key, set);
+ } else {
+ // Word in map. Just add the new index.
+ indices.add(value);
+ }
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.