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

Abdelrahman Eltahir
PLUS
Abdelrahman Eltahir
Courses Plus Student 2,314 Points

compiler tells gives me an error of "it looks like task 1 is no longer passing" . and i don't get it

can someone please tell me where i've went wrong

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.
  if (obj instanceof Object) {
         BlogPost result = (BlogPost) obj;
          return result.getTitle(); 
     } else{
       return "";
     }   
  }
}

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hi there,

Well for the first challenge you want to check if it is an instance of a String.

obj instanceof Object

That line is useless as our obj is already an object. In the paramater, we pass in an Object right? So whatever we pass in, whether it be a string or something else, it will be automatically upcasted to an Object. Since Object is really vague as everything is an object, we want to check if the thing we passed in is a string. So it would look like this:

if (obj instanceof String) {
      return (String) obj;
    } 

For the second challenge, you got this piece of code:

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

You want to change that Object to BlogPost. So if we indeed passed in a BlogPost object, we can downcast it appropriately and get the title from it.

I think you should know know how to solve it from here. If you still need help, let me know :smile: !

Happy coding and best of luck,

Kevin

Abdelrahman Eltahir
Abdelrahman Eltahir
Courses Plus Student 2,314 Points
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.


     if(obj instanceof BlogPost){
    return ((BlogPost) obj).getTitle();
    }else { return "";}
     }  
}
//this is my code for task 2 and it still says that "Oops! It looks like Task 1 is no longer passing." .
Kevin Faust
Kevin Faust
15,353 Points

Well obviously you will get that error as where did part 1 of the challenge go? Did you delete it? It works completely fine in terms of the second part of the challenge...

AddThis.java
   if (obj instanceof String) {
      return (String) obj;
    } 

Im assuming you passed task 1 but then you deleted it for task 2. You want to keep both

Abdelrahman Eltahir
PLUS
Abdelrahman Eltahir
Courses Plus Student 2,314 Points

Yes kevin your Assumption was correct , i used to delete the answer of task 1 everytime i passed it which was very silly of me , don't know why i did that , Thanks for your help i really appreciate it.

Kevin Faust
Kevin Faust
15,353 Points

Well glad we got it working and glad to help :smile: