Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Enyang Mercy
Courses Plus Student 2,339 PointsDesigning UI Fix the addTag and addTags
Stuck again. Please check my code and suggest answers
package com.example.model;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
public class Course {
private String mTitle;
private Set<String> mTags;
public Course(String title) {
mTags = new HashSet();
mTitle = title;
// TODO: initialize the set mTags
}
public void addTag(String tag) {
mTags.add(tag);
// TODO: add the tag
}
public void addTags(List<String> tags) {
for(String tag : tags) {
mTags.addTags(tag);
// TODO: add all the tags passed in
}
public boolean hasTag(String tag) {
// TODO: Return whether or not the tag has been added
return false;
}
public String getTitle() {
return mTitle;
}
}
5 Answers
Enyang Mercy
Courses Plus Student 2,339 PointsSomething is wrong again i just closed my for loop but it still cant compile think i didnt declare the new string variable
Steve Hunter
57,712 PointsYou haven't closed your for loop.
public void addTags(List<String> tags) {
for(String tag : tags) {
mTags.addTags(tag);
} // <- add this
// TODO: add all the tags passed in
}
Plus, inside the method call addTag(tag) - [edit] your code is calling addTag on mTags. It shouldn't. Just call it straight like:
public void addTags(List<String> tags) {
for(String tag : tags) {
addTags(tag);
}
}
The addTag method manages the mTags bit.
Steve.
Enyang Mercy
Courses Plus Student 2,339 PointsFor the third question is this ok? if (hasTag.add == mTags) return true
Steve Hunter
57,712 PointsHi there,
For the third task you want to return true if the tag is contained in mTags, else false. The List class comes with a handy method called .contains() that does this for you.
You want to use .contains() on mTags using dot notation and pass in the parameter, tag. As this will resolve to a true/false value for you - just return that expression. There's no need for interim variables or if statements:
public boolean hasTag(String tag) {
// TODO: Return whether or not the tag has been added
return mTags.contains(tag);
}
Make sense
Steve.
Enyang Mercy
Courses Plus Student 2,339 PointsGreat !! thanks
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsYes, you have - you did that with
String tagin the for loop brackets. Have you removedmTagsfrom the line inside the loop? Look at my second code block.