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

Ronit Mankad
Ronit Mankad
12,166 Points

Logical error

I am getting error that " Everything is correct but the 1 st task wont pass". I added return result; inside the if statement because i was not able to ask for help with the above error(some sort of bug ).

Can anyone tell me what logical mistake im doing?. Thanks in advance!

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

  }
}

2 Answers

I think that is better placed inside a single if/else statement. The question states that the method can only be passed a BlogPost or a String, so a failed test for one can safely assume the other. Have a look at my post here that makes that assumption too. (So I'm agreeing with myself!!).

So, we can do this in a few lines of code - this is the same as above, just a bit neater:

    if(obj instanceof String){
      return (String) obj;
    } else {
      return ((BlogPost) obj).getTitle();
    }

Steve.

Hi Ronit,

Let's start with the string part; you want to use instanceof inside the if statement to check that obj is a String. If it is, cast it then return it:

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

A similar process can be done with the BlogPost task. I did this as a separate if statement - I don't see that they need to be joined but, assuming mutual exclusivity (i.e. obj can only be a String or a BlogPost), you can use the else clause of the above if statement. I opted for the safer method and tested for BlogPost separately. I did the same as with the String - use instanceof to test if obj is a BlogPost, if it is, follow the instructions; 1. cast to BlogPost, 2. return the result of calling the getTitle() method on it. In one line, that look like:

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

I hope that helps.

Steve.

Ronit Mankad
Ronit Mankad
12,166 Points

Thanks Very Much!. Adding another if statement works!