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

Sam Millington
Sam Millington
7,537 Points

Oops! It looks like Task 1 is not running. Why is this message appearing on task 2?

Trying to solve task 2, and when I click 'check work' it tells me that task 1 has stopped working. When I go back to task 1 and check whether it works, it works straight away.

I've attached my code.

Thanks, Sam

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 = true;

  public static String getTitleFromObject(Object obj) {

    String result = "";
    // Fix this result variable to be the correct string.
    if (obj instanceof com.example.BlogPost) {
    BlogPost post = (BlogPost)obj;
     result = post.getTitle();
    return result;
   }

     return result;
   }

}

2 Answers

Hi Sam,

I can't see Task 1 code in there? That should test if obj is instanceof String and return it cast as a String by assigning it into the result variable. The result variable is initialised to a null string before we use it and is returned at the end. You need to leave Task 1 code there for the second part of the challenge.

Task 2 requires you to do a similar thing, i.e. set the result variable after testing if obj is an instanceof a BlogPost. The slight difference is you need to call a method, getTitle() on it before assigning into result, otherwise it wouldn't 'fit' as obj is a BlogPost not a String - the getTitle() method returns a String. So, do the instanceof test then inside the if cast obj to BlogPost then surround all that in brackets and chain, using dot notation, the getTitle() method on to it. Assign that into result.

At the end of the method, result gets returned, whatever value it holds.

That all looks like:

  public static String getTitleFromObject(Object obj) {

    String result = "";
    if (obj instanceof String){
      result = (String) obj;
    }

    if (obj instanceof BlogPost){
      result = ((BlogPost) obj).getTitle();
    }
    return result;
  }

I hope that makes sense.

Steve.

Sam Millington
Sam Millington
7,537 Points

Hi Steve,

Yes, this was the answer. I removed the code from task 1 and replaced it with the code for task 2. So I had nothing to handle passing Strings into the method.

Thanks,

Sam

Glad you got it fixed. :+1: :smile: