Example #1
0
  /*
  Method: parseInputLine - text parser
  Purpose: parse the line of text and returns a TreeNode
  Parameters:
      String line   the line of text being parsed
  Returns:
      Entry - the Object parsed from the text line
  */
  private TreeNode parseInputLine(String line) {
    if (line.equals("")) {
      // returns a null object if the line is empty
      // only null entry source in code
      return null;
    }

    // a new empty TreeNode object
    TreeNode returnNode = new TreeNode(ENTRY); // is an entry
    // Scanner to scan the line of text
    Scanner lineScanner = new Scanner(line);
    lineScanner.useDelimiter("/");

    // sets the entry's word
    returnNode.setMyString(lineScanner.next());

    while (lineScanner.hasNext()) {
      // the next word in the line
      String nextWord = lineScanner.next();
      // end of line and 'a//b/c' blank words
      if (!(nextWord == null) && !(nextWord.equals(""))) {
        // adds each word in alphabet order to the
        // synonym linkedList
        returnNode.addSynonymInOrder(nextWord);
        // might not have any synonyms
      }
    }
    // returns the finished entry object
    return returnNode;
  }
Example #2
0
  /*
  Method: addSynonymFromMenu - database addition
  Purpose: adds a synonym to an existing entry
  Parameters:
      String wordToAddTo      the word to update
  Returns:
      none - the results are printed here
  */
  public void addSynonymFromMenu(String wordToAddTo) {
    if (wordToAddTo.equals("")) {
      // if the word is empty go back to caller
      System.out.println("\nPlease type a valid word\n");
      return;
    }

    // checks whether the word is in the database and gets it
    if (!this.containsEntry(wordToAddTo)) {
      // if the word isn't there, go back to caller
      System.out.println("Sorry, I could not find that word in the thesaurus\n");
      return;
    }
    TreeNode entryToModify = TreeNode.getNodeByString(wordToAddTo, entryRoot);

    // controls if the loop is repeated
    boolean repeatCheck = true;
    while (repeatCheck) {
      System.out.println("What synonym would you like " + "to add to the word: " + wordToAddTo);
      System.out.print(">> ");
      // Scanner to take user input
      Scanner inputScanner = new Scanner(System.in);
      // the synonym to be added
      // will accept spaces in words
      String userInput = inputScanner.nextLine();

      if (userInput.equals("")) {
        System.out.println("Sorry, I can't add nothing\n");
        // jump down to loop control
      } else {
        // checks to see if the entry already has that synonym

        if (entryToModify.containsSynonym(userInput))
          System.out.println(wordToAddTo + " already has that synonym\n");
        else {
          // adds the synonym to the entry
          entryToModify.addSynonymInOrder(userInput);
          System.out.println(userInput + " was added to the entry\n");
        }
      }

      // loop control menu
      System.out.println("\nWould you like to add another synonym?");
      System.out.print("type (yes/no) >>");
      // does the user want to add another entry?
      String userResponse = inputScanner.next();
      if (userResponse.equalsIgnoreCase("yes")) {
      } else {
        System.out.println("\nGoing back to the main menu\n");
        repeatCheck = false;
      }
    }
  }