Reputation: 381
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;
}
}
Upvotes: 0
Views: 317
Reputation: 124225
You shouldn't use getWordSet()
on quoteOne
since it is String[]
and it doesn't have such method. You probably want to use it on wordgroupOne
which is WordGroup
type. Same goes for quoteTwo
.
Also you should prefer programming on interfaces rather than actual classes if possible so maybe change your method to return Set
rather than HashSet
.
How would I go about putting both results in the same hashset? I tried changing them to both hashSOne and it just wrote over my first hashset with the second
You probably did
hashSOne = hashSTwo;
which just makes hashSOne
reference use set from hashSTwo
reference.
If you want to create new set that will contain all items from both sets you can do it this way
//lets create Set with elements from first set
Set<String> allElements = new HashSet<>(hashSOne);
//now lets add all elements from second set
allElements.addAll(hashSTwo);//this will not add duplicates
Upvotes: 1
Reputation: 11440
You should definitely be more specific in what the error is for future posts. It can be very difficult to find what the error is without the compilers help.
HashSet<String> hashSOne = quoteOne.getWordSet();
HashSet<String> hashSTwo = quoteTwo.getWordSet();
should be
HashSet<String> hashSOne = wordgroupOne.getWordSet();
HashSet<String> hashSTwo = wordgroupTwo.getWordSet();
Upvotes: 0