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 trialAttila Tamás
1,168 PointsType casting
I got stuck at the second challenge of Type Casting. Can someone please help?
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;
}
}
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
Wes Grant
4,642 PointsFor 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;
}
Wes Grant
4,642 PointsFirst 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
1,168 Pointsit threw me this error message:
I expected the value from getTitle on the BlogPost instance, but instead I got ''
Wes Grant
4,642 PointsMaybe 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
1,168 PointsAh 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?
Farai Ted Mandoreba
21,518 Pointsguys help me on that question i am totally stuck now and clue less tried all i know, help me
Attila Tamás
1,168 PointsAttila Tamás
1,168 Pointsthank you so much for your help ^^
mongst
10,841 Pointsmongst
10,841 PointsI had to add one more "}" at the end to get the above to work. +1 to Wes Grant for getting me that far though.