I am trying to use the getWordSet() method from my WordGroup class on the 2 wordgroups in my Main class and loop over the hashset returned and print out the result but it isn't even compiling. My attempt at the code which I hoped would do it is in the bottom of my main class between the asterisks. Can somebody help me please? EDIT: I have implemented teh changes suggested as shown in my code below and this has lead to a new problem- I want both wordgroups to go into the same set but when I tried changing hashSTwo to hashSOne in an attempt to get all teh words into hashSOne it wouldn't compile.
import java.util.HashSet;
import java.util.HashMap;
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);
}
**HashSet<String> hashSOne = wordgroupOne.getWordSet();
HashSet<String> hashSTwo = wordgroupTwo.getWordSet();
for (String set : hashSOne){
System.out.println(set);
}
for (String set : hashSTwo){
System.out.println(set);
}**
}
}
WordGroup set:
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<String> getWordSet(){
HashSet<String> set = new HashSet<String>();
String[] p = getWordArray();
for (String items : p){
set.add(items);
}
System.out.println(set);
return set;
}
public HashMap<String, Integer> getWordCounts() {
HashMap<String, Integer> map = new HashMap<String, Integer>();
String[] q = getWordArray();
for (String stuff : q) {
Integer oldVal = map.get(stuff);
if (oldVal == null){
oldVal = 0;
}
map.put(stuff, oldVal+1);
}
System.out.println(map);
return map;
}
}