Click here to Skip to main content
15,844,673 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In this method The wordPair method The two parameters represent two case-sensitive
words that might have been found in a document. The method must return
true if the first parameter has occurred at least once in the document and was
immediately followed at least once by the second word. Otherwise it must
return false.
In other words, the method must return true if at some point, a call was made
to addWord with the first parameter, and the next call to addWord was made
with the second parameter. Otherwise it must return false. See below for an
example.


What I have tried:

This is my code with the attempt i have made but it is not working, i dont think i should have split up strings.
Java
<pre>import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

/**
 * Keep track of word counts and word pairs.
 * 
 * DO NOT CHANGE ANY OF THE METHOD NAMES, PARAMETERS
 * OR RETURN TYPES IN THIS FILE. IF YOU DO, IT WILL
 * NOT BE POSSIBLE TO MARK YOUR WORK.
 *
 * @author 
 * @version 
 */
public class WordTracker
{   
    private HashMap<String, Integer> numberOfWords;
    private HashMap<String, Boolean> wordPairs;
    private String previousWord;

    /**
     * Constructor for objects of class WordTracker
     */
    public WordTracker()
    {
        numberOfWords = new HashMap<>();
        wordPairs = new HashMap<>();
    }

    /**
     * Add a word to the analyser.
     * @param word the word to be added.
     */
    public void addWord(String word) {
        if (numberOfWords.containsKey(word)) {
            int count = numberOfWords.get(word);
            numberOfWords.put(word, count + 1);
        } else {
            numberOfWords.put(word, 1);
        }
    }

    /**
     * Get the number of times the given word has been seen.
     * @param word The word to be looked up.
     * @return The number of times the word has been seen.
     */
    public int getCount(String word)
    {
        if (numberOfWords.containsKey(word)) {
            return numberOfWords.get(word);
        } else {
            return 0;
        }
    }

    /**
     * Return true if firstWord is immediately followed
     * by secondWord in the original text; false otherwise.
     * In other words, return true if, after a call to addWord(firstWord),
     * the next call to addWord was addWord(secondWord).
     * 
     * @param firstWord A possible word added immediately before secondWord.
     * @param secondWord A possible word added immediately after firstWord.
     */
    public boolean wordPair(String firstWord, String secondWord) {
        String document = ""; //get the document as a string
        String[] words = document.split(" "); //split the document into an array of words
        boolean firstWordFound = false;
        for (int i = 0; i < words.length - 1; i++) {
            if (words[i].equals(firstWord)) {
                firstWordFound = true;
                if (words[i + 1].equals(secondWord)) {
                    return true;
                }
            }
        }
        return firstWordFound;
    }
Posted
Updated 19-Jan-23 23:47pm
Comments
Richard MacCutchan 19-Jan-23 10:43am    
What does "not working" mean? As far as I can see the wordPair method will return true if it finds the first word, whether it finds the second or not. Your boolean flag should only be set to true if both words are found together.
Peter_in_2780 21-Jan-23 19:08pm    
... and it'll struggle to find anything in the empty document it created...
Richard MacCutchan 22-Jan-23 3:56am    
... I deliberately left that as an exercise for the OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900