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

Romel Liwag
Romel Liwag
4,183 Points

Type Casting Challenge

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.

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

    if(obj instanceof String){
      HINT_1_ENABLED = true;
      HINT_2_ENABLED = true;

    }
    return "String";
  }
}

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Romel;

Welcome to Treehouse!

You don't want to move the hint code into the method. If you want to turn them on, set them to true, one at a time, and make sure you don't have any errors in the code you add. For example, to see Hint 1:

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 = true;
  public static boolean HINT_2_ENABLED = false;

  public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
    return "";
  }
}

Post back if you are still stuck once you view the hints.

Ken

Romel Liwag
Romel Liwag
4,183 Points

Im quite confused at start but I figured out how to do it :) this forum is very helpful :D

Hey!

Sorry I'm new here. Is this a challenge made by you or a challenge you need help with. Because I can't see any concrete question in your posting.

greets Charlie

Romel Liwag
Romel Liwag
4,183 Points

Java type casting Challenge, but i found out how to do it eventually so it s Good :)