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 Getting There Type Casting

I expected the String value I passed in to be returned and it wasn't. Received '

Why am I not returning a result ?

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost {
    private String mAuthor;
    private String mTitle;
    private String mBody;
    private String mCategory;
    private Date mCreationDate;

    public BlogPost(String author, String title, String body, String category, Date creationDate) {
      mAuthor = author;
      mTitle = title;
      mBody = body;
      mCategory = category;
      mCreationDate = creationDate;
    }

    public String getAuthor() {
      return mAuthor;
    }

    public String getTitle() {
      return mTitle;
    }

    public String getBody() {
      return mBody;
    }

    public String getCategory() {
      return mCategory;
    }

    public Date getCreationDate() {
      return mCreationDate;
    }
}
TypeCastChecker.java
import com.example.BlogPost;

public class TypeCastChecker {
  /***************
  I have provided 2 hints for this challenge.
  Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
  NOTE: You must set all the hints to false to complete the exercise.
  ****************/
  public static boolean HINT_1_ENABLED = false;
  public static boolean HINT_2_ENABLED = false;

  public static String getTitleFromObject(Object obj) {
    // Fix this result variable to be the correct string.
     String result= ""; 
    if (obj instanceof BlogPost) {
          BlogPost bp = (BlogPost) obj;  
           result = bp.getTitle();             
}
      if (obj instanceof BlogPost) {
      BlogPost bp = (BlogPost) obj;
        result = bp.getTitle();  }
return result;

  }
}

1 Answer

Hi Tyree,

It looks like your code is no longer passing task 1. Keep the conditional for checking if obj is an instanceof String. If so, assign obj type cast as String to result.

For task 2, add an else if conditional that checks if obj is an instanceof BlogPost. If so, assign to result the return value from calling getTitle() on obj after type casting to BlogPost.

public static String getTitleFromObject(Object obj) {
  String result = "";
  if (obj instanceof String) { result = (String) obj; }
  else if (obj instanceof BlogPost) { result = ((BlogPost) obj).getTitle(); }
  return result;
}

Please let me know if this helps or not.

Cheers

thanks

how did you figure out the answers to both activities.

This challenge is all about type casting. Once you understand what type casting is, how to do it, and how to check if a generic object is an instance of some class - then this challenge is pretty easy.

If it's not quite clicking yet - and I completely understand that it may take a while - consider re-watching the previous videos. Also, search the internet with the terms "java type casting", follow some of those links and study. Read code written by others.

Gradually, it will start to make more sense. If you rush the learning process, you may get frustrated.

I hope this was useful advice.