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

Totally stumped on challenge task 1 of 2 for casting

Hi,

I'm having lots of difficulty with this challenge. If you notice the code I added starts right after the comment about fixing this result variable. Here is my code. Thanks for the help!

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.
   public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
    if (obj instanceof String) {
      return (String) obj;
    } else {
      return ((BlogPost) obj).getTitle(); 
    }


  }
}

1 Answer

Christopher Augg
Christopher Augg
21,223 Points

Hello Gabriel,

The issue you are having is due to the return statement being wrapped within the else clause. A method that requires a returned type must have a return statement that is not out of scope. While we can write code with multiple return statements, the challenge is asking for you to stick to a good code writing convention that calls for only using one return. Furthermore, it is teaching a better way to cast variables for their use within the method.

Here are some examples:

An altered version of your code that allows it to work:

 public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
    if (obj instanceof String) {
      return (String) obj;
    } else if (obj instanceof BlogPost) {
      return ((BlogPost) obj).getTitle(); 
    }
      return "";  // notice that we have to provide the ability to get to this line and return outside of the if/else block
  }

However, the challenge provided the String result for the purpose of teaching a better code writing convention. Here is an example:

 public static String getTitleFromObject(Object obj) {
    // We provide a default String to return if we do not get one of the desired objects
    String result = "";
    if (obj instanceof String) {
     // If obj is an instance of String, we cast it to a String and assign it to result
       result = (String)obj;
    } else if (obj instanceof BlogPost) {
     // If obj is an instance of a BlogPost, we cast it to a BlogPost and assign it to post
      BlogPost post = (BlogPost)obj;
     // Now that we have a BlogPost named post. We can call its method to get the title
     // and assign it to result 
      result = post.getTitle();
    }
    // We only have one return statement. This allows our code to be much clearer.
    return result;
  }

Please let me know if this helps.

Regards,

Chris

Thanks Christopher,

That actually helped my understanding of it better. I should have taken my post off the forum, because I just figured it out a couple hours ago. Thanks anyway!

Christopher Augg
Christopher Augg
21,223 Points

No problem Gabriel. I am glad you got it. Way to go!