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

Java Java Data Structures - Retired Efficiency! Changing Course

Irving Amador
Irving Amador
7,488 Points

Java Data Structures last challenge cant use setTtitle method

I am stuck in this challenge because i cant set the new title of the video, i dont know how to use the setTitle method from the Video class.

I think the problem is when i get the value of the video from the TreeMap that it returns an object but im not sure.

Any help would be appreciated.

com/example/model/Course.java
package com.example.model;

import java.util.List;

public class Course {
  private String mName;
  private List<Video> mVideos; 

  public Course(String name, List<Video> videos) {
    mName = name;
    mVideos = videos;
  }

  public String getName() {
    return mName;
  }

  public List<Video> getVideos() {
    return mVideos;
  }

}
com/example/model/Video.java
package com.example.model;

public class Video {
  private String mTitle;

  public Video(String title) {
    mTitle = title;
  }

  public String getTitle() {
    return mTitle;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

}
QuickFix.java
import com.example.model.Course;
import com.example.model.Video;

import java.util.Map;
import java.util.List;
import java.util.TreeMap;

public class QuickFix {
  Map videosHash = new TreeMap();

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video video = new Video("The Beginning Bits");

    // TODO(2):  Add the newly created video to the course videos as the second video.
    List<Video> videos = course.getVideos();
    videos.add(1, video);
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
    videosByTitle(course);
    Object obj = videosHash.getKey(oldTitle); // Get the value of the map, not the key

    obj.setTitle(newTitle); //Cant set video title, how to use setTitle method?
  }

  public Map<String, Video> videosByTitle(Course course) {
    for (Video video : course.getVideos()) {
      videosHash.put(video.getTitle(), video);
    }
    return videosHash;
  }

}
Irving Amador
Irving Amador
7,488 Points

I changed the code a bit in the fixVideoTitle method inside QuickFix.java, like this:

public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
    videosByTitle(course);
    videosHash.get(oldTitle).setTitle(newTitle);
  }

And also changed the way i was declaring the MapTree variable, like this:

public Map<String, Video> videosHash = new TreeMap<String, Video>();

And so now im getting this error:

Bummer! Hmmm...I found the old value 'Typo Casting' and the new value 'Type Casting'. You should 'get' the video from the map and update the video using 'setTitle'

What is wrong with this code?

1 Answer

Irving Amador
Irving Amador
7,488 Points

The problem was i was using TreeMap instead of HashMap.

Here is how it passed in the end:

import com.example.model.Course;
import com.example.model.Video;

import java.util.Map;
import java.util.List;
import java.util.HashMap;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video video = new Video("The Beginning Bits");

    // TODO(2):  Add the newly created video to the course videos as the second video.
    List<Video> videos = course.getVideos();
    videos.add(1, video);
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {
    videosByTitle(course).get(oldTitle).setTitle(newTitle);
  }

  public Map<String, Video> videosByTitle(Course course) {
    Map<String, Video> videosHash = new HashMap();
    for (Video video : course.getVideos()) {
      videosHash.put(video.getTitle(), video);
    }
    return videosHash;
  }

}