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.
<pre>import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class WordTracker
{
private HashMap<String, Integer> numberOfWords;
private HashMap<String, Boolean> wordPairs;
private String previousWord;
public WordTracker()
{
numberOfWords = new HashMap<>();
wordPairs = new HashMap<>();
}
public void addWord(String word) {
if (numberOfWords.containsKey(word)) {
int count = numberOfWords.get(word);
numberOfWords.put(word, count + 1);
} else {
numberOfWords.put(word, 1);
}
}
public int getCount(String word)
{
if (numberOfWords.containsKey(word)) {
return numberOfWords.get(word);
} else {
return 0;
}
}
public boolean wordPair(String firstWord, String secondWord) {
String document = "";
String[] words = document.split(" ");
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;
}