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

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

don't know were to start on this one...don't know what he's asking for

i have no idea what to put where...How do i do type casting in java?

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 = "a string value...";
    return result;
  }
}

4 Answers

Hi Luis, You're really close. You want to return the obj cast as a String in this example. This is supposed to go inside the if statement.

return (String) obj;

The result is there as the default return statement. If the obj isn't an "instanceof" a String then the method returns an empty String.

Luis Cole
Luis Cole
17,228 Points

Thanks for the help Rajen!

If you don't know where to begin, I would recommend watching the video again. But for a place to start, He is asking for you to cast the obj as a String if the obj passed is a String.

Here's an example of type casting. String name = (String) object; This is casting the object as a String. Your job is to figure out whether or not the obj passed was a string. Then to return the object cast as a string if the object is in fact a string.

Hope this helps, if not, I can explain more.

Luis Cole
Luis Cole
17,228 Points

This is my method code:

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;
}

I don't if this is right but I'm getting this errors:

./TypeCastChecker.java:16: error: ')' expected if(obj instanceOf String){ ^ ./TypeCastChecker.java:16: error: ';' expected if(obj instanceOf String){ ^ ./TypeCastChecker.java:16: error: variable declaration not allowed here if(obj instanceOf String){ ^ ./TypeCastChecker.java:16: error: incompatible types: Object cannot be converted to boolean if(obj instanceOf String){ ^ ./TypeCastChecker.java:16: error: cannot find symbol if(obj instanceOf String){ ^ symbol: class instanceOf location: class TypeCastChecker 5 errors

Hey brother! I had the same problem and the issue were all having is that we are writing instanceOf with a capital "O" vs instanceof with a lowercase "o". Hope that helps!

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

sorry it took so long getting back. i've decided to step away from this Java track for the moment. thank you for your help though.