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 trialJames Croxford
12,723 PointsChallenge Task 3, final challenge at the end of Java Data Structures- why is my fixVideoTitle method not working?
Hi everyone,
I'm currently looking at the following challenge, specifically task 3:
"Finally, lets finish that final method fixVideoTitle. I will pass in the course, the current title, and the title I want to change it to. Hey you should totally use that videosByTitle map to help you find the video!"
Whilst I've managed to complete it now, why does my strategy NOT work? I assumed that the replace method I've took from the HashMap documentation would fit the bill perfectly, but not so it seems?
I assume it is because the method requires input of
(Course course, String oldTitle, String newTitle)
But I've tried to feed it a title, which is not a Course? Is there any way I could use the replace method on HashMaps to complete this task?
import com.example.model.Course;
import com.example.model.Video;
import java.util.HashMap;
import java.util.Map;
public class QuickFix {
public void addForgottenVideo(Course course) {
// TODO(1): Create a new video called "The Beginning Bits"
Video newVideo = new Video("The Beginning Bits");
// TODO(2): Add the newly created video to the course videos as the second video.
course.getVideos().add(1, newVideo);
}
public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
for (Video video : course.getVideos()) {
videosByTitle.replace(video.getTitle(), oldTitle, newTitle);
}
}
public Map<String, Video> videosByTitle(Course course) {
Map<String, Video> byTitle = new HashMap<>();
for(Video video : course.getVideos()) {
byTitle.put(video.getTitle(), video);
}
return byTitle;
}
}