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

Attila Tamás
Attila Tamás
1,168 Points

Type casting

I got stuck at the second challenge of Type Casting. Can someone please 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(com.example.BlogPost obj) {
    // Fix this result variable to be the correct string.
    //initialise the variable
    String result = "";
    //check if it's a string
     if(obj instanceof String)
    {
        BlogPost blog = (BlogPost) obj;
        String result = blog.getTitle();
    }

    //return the result variable. if the obj is not a string, it'll return the empty ""
        return (String) obj;

  }
}

4 Answers

For the second part of the challenge you are going to want to add an else clause to your if statement that will test whether the obj is an instanceof BlogPost. ... And if it is, you cast the Object to a BlogPost and then get its title:

public static String getTitleFromObject(Object obj) {
  // Fix this result variable to be the correct string.
  String result = "";
  if(obj instanceof String) {
    result = (String) obj;
  } else {
    BlogPost obj2 = (BlogPost) obj;
    result = obj2.getTitle();
  }
  return result;
}
Attila Tamás
Attila Tamás
1,168 Points

thank you so much for your help ^^

I had to add one more "}" at the end to get the above to work. +1 to Wes Grant for getting me that far though.

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

First off,

In your method declaration,

public static String getTitleFromObject(com.example.BlogPost obj) {

obj needs to be declared as Object and not a BlogPost so that you can pass in either a Blogpost OR a String. Object is a superclass of String and BlogPost so Object is legal for both BlogPost or String.

This task is simply asking you to return the passed in obj as String if it is in fact a string so you will want to do something like this:

public static String getTitleFromObject(Object obj) {
  // Fix this result variable to be the correct string.
  String result = "";
  if(obj instanceof String) {
    result = (String) obj;
  }
  return result;
}
Attila Tamás
Attila Tamás
1,168 Points

it threw me this error message:

I expected the value from getTitle on the BlogPost instance, but instead I got ''

Maybe you are on the 2nd part of the challenge? The directions for the 1st part of the challenge (which is linked to this question) state:

The method getTitleFromObject will be called and passed a String and/or a com.example.BlogPost. For this first task, return the object obj type casted as a String if it is in fact a String. 

I've included BlogPost.java for your reference only.
Attila Tamás
Attila Tamás
1,168 Points

Ah yes, i had problem with the second challenge ^^ The first one wasn't hard. but i'm having problems with the second one, as said in the description. Can you help with the second one?

guys help me on that question i am totally stuck now and clue less tried all i know, help me