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 trialmicahgreen
1,027 PointsHaving trouble with the "hasTag" method in Course.java.
I have tried several different solutions for the "hasTag" method problem, but none have worked. I have used the .contains method from the Set class and also the .contains method. Most recently, as you'll see below, I tried using an if-else statement and that didn't work. I deleted the else part of the statement and decided to give up and ask the forum. The current answers in the forum didn't help me with question 3 of the problem. Can someone help me find a good solution? I've gotten this far in the java training without getting stuck and finally hit a road block :( Thanks for any help you can give!
package com.example.model;
import java.util.List;
import java.util.Set;
import java.util.HashSet;
import java.util.ArrayList;
public class Course {
private String mTitle;
private Set<String> mTags;
public Course(String title) {
mTitle = title;
// TODO: initialize the set mTags
mTags = new HashSet<String>();
}
public void addTag(String tag) {
// TODO: add the tag
mTags.add(tag);
}
public void addTags(List<String> tags) {
// TODO: add all the tags passed in
mTags.addAll(tags);
}
public boolean hasTag(String tag) {
// TODO: Return whether or not the tag has been added
if (mTags.contains(tag)) {
System.out.println("Tag already exists");
}
return false;
}
public String getTitle() {
return mTitle;
}
}
1 Answer
Craig Dennis
Treehouse TeacherWhat if you return the result of that expression?
micahgreen
1,027 Pointsmicahgreen
1,027 PointsThis seemed to fix it:
Although I'm sure there's a much better way. I have a long way to go to truly understand the syntax. Thanks for your help Craig! I really enjoy your teaching.
Sincerely, Micah Green
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherRemember you can return the result of an expression like so:
return mTags.contains(tag);
micahgreen
1,027 Pointsmicahgreen
1,027 PointsEven better! Thanks again Craig!