Note that there are some explanatory texts on larger screens.

plurals
  1. POPutting words from an array into a HashMap and a HashSet
    text
    copied!<p>I am coding in blueJ and what I am trying to do is this: </p> <p><strong>1.a)</strong> Create a <code>getWordSet()</code> method in <code>WordGroup</code> which:</p> <ul> <li>takes another <code>WordGroup</code> object as a parameter</li> <li>creates <code>a HashSet&lt;String&gt;</code></li> <li>uses two for loops to put all the words from this and the parameter <code>WordGroup</code> into the <code>HashSet</code></li> <li>returns the <code>HashSet&lt;String&gt;</code></li> </ul> <p><strong>1.b)</strong> In the <code>main</code> method:</p> <ul> <li>use the <code>getWordSet()</code> method using the two <code>WordGroup</code>s</li> <li>iterate or loop over the <code>HashSet</code> returned and print the words from it</li> </ul> <p><strong>2.a)</strong> Create a method in the <code>WordGroup</code> called <code>getWordCounts()</code> which:</p> <ul> <li>creates a <code>HashMap&lt;String, Integer&gt;</code></li> <li>loops over all the words returned by <code>getWordArray()</code> and puts each word into the <code>HashMap</code> with the number of times it occurs</li> <li>returns <code>HashMap&lt;String, Integer&gt;</code></li> </ul> <p><strong>2.b)</strong> In the <code>main</code> method:</p> <ul> <li>call <code>getWordCounts()</code> on the two <code>WordGroup</code>s</li> <li>use <code>keySet()</code> to retrieve the set of keys (the String part of the mapping)</li> <li>loop over this set and print out the word and its count for both <code>WordGroup</code>s </li> <li>use the <code>getWordSet()</code> method to make complete set of all the words from both <code>WordGroup</code>s</li> <li>loop over the new <code>HashSet</code> to print a complete list of all words with the sum counts from each of the <code>HashMap</code>s</li> </ul> <p>My code so far: </p> <pre><code>public class Main{ public static void main(String[] args){ WordGroup wordgroupOne= new WordGroup ("You can discover more about a person in an hour of play than in a year of conversation"); WordGroup wordgroupTwo= new WordGroup ( "When you play play hard when you work dont play at all"); String[] quoteOne = wordgroupOne.getWordArray(); String[] quoteTwo = wordgroupTwo.getWordArray(); for (String words : quoteOne){ System.out.println(words); } for (String words : quoteTwo){ System.out.println(words); } } } </code></pre> <p>WordGroup class:</p> <pre><code>import java.util.HashSet; import java.util.HashMap; public class WordGroup { public String words; public WordGroup (String getWords){ words = getWords.toLowerCase(); } public String[] getWordArray(){ return words.split(" "); } public HashSet&lt;String&gt; getWordSet(){ HashSet&lt;String&gt; set = new HashSet&lt;String&gt;(); for (String words : quoteOne){ words.add(word); } return words; } public HashMap&lt;String, Integer&gt; getWordCounts() { HashMap&lt;String, Integer&gt; map = new HashMap&lt;String, Integer&gt;(); for (String words : words) { words.add(word); } return HashMap&lt;String, Integer&gt;; } } </code></pre> <p>I have got this far and now I am stuck. I cannot figure out how to get the words from teh array into the hashset and hashmap and how to return them in the desired form. p.s. sorry about the wierd question layout- the string kept disappearing after hashset if it was not in the code format)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload