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

Stuck on Challenge (Type Casting @ Java Data Structures)

Here is instructure:

Challenge Task 1 of 1

The method getTitleFromObject will be called and passed a String and/or a com.example.BlogPost. Return the object type casted as a String if it is a String, and if it is the BlogPost type cast it, and return the results of the getTitle method.

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 return statement to be the correct string.
    return "";
  }
}

Thanks for any insight!

Check out this forum post:

https://teamtreehouse.com/forum/casting-from-a-type-object-to-a-type-string

I was really stuck on this too. I was separating the return statement and the type casting of the blogpost object.

6 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Abdullah;

Did you resolve this or would you like further discussion? Also, before I go through this how comfortable are you with the concept of casting in general? I can attempt to explain that as well, if need be, to help better understand things if you are stuck.

Casting is not my strong suit so you may have to bear with my explanation, but I'm willing to give it a try.

Post back when you get a chance.

Ken

Ken;

I got my problem and solved it. Thanks for your answers. I understood the concept of casting with super and child classes. Before I start this Java course, I only casted basic data types and I really didn't need it. But this challenge changed my idea.

Thanks again.

The challenges towards the end of the Java Objects track became very poorly worded, not explaining clearly what you had to do to complete the challenge.

This one suffers from the same problem. I spent more time trying to work out what it was I supposed to be doing than actually solving it.

Exactly what I felt. I've done C, assembly and some Java in the past and recognize a poor explanation when i see one..

Up!

Hello Alex, /* Check out the else if block, you have casted Object obj into BlogPost but later, you are typeCasting to a String, the String does not have a getTitle() method. So, basically you need to typeCast your object into Blogpost. This Snippet should work as per my understanding: */ else if(obj instanceof BlogPost){ BlogPost obj1 = (BlogPost) obj; result = obj1.getTitle(); } return result;

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Abdullah;

What is your question exactly? The task is to return the correct object based on it's type, right?

If you use the built in hints Mr. Dennis provided for our use that may help point you in the correct direction.

Post back if you are still stuck.

Ken

I'm having trouble with this challenge as well, I'm not sure exactly what the challenge is asking me to do. This is how I tried to solve it:

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 return statement to be the correct string.
    if (obj instanceof String){
      return (String) obj;
    }
    else if (obj instanceof BlogPost){
      obj = (String) obj;
      return getTitle();
    }
  }
}
./TypeCastChecker.java:21: error: cannot find symbol
      return getTitle();
             ^
  symbol:   method getTitle()
  location: class TypeCastChecker
1 error