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 trialsoo ao long tian
1,111 PointsHelp, i don't understand how to solve this exercise.
Help, i don't understand this quiz
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 = true;
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 String) {
return result;
}
}
}
2 Answers
Benjamin Barslev Nielsen
18,958 Pointsobj can be either a String or a BlogPost. In the first task of the challenge you are supposed to check if obj is in fact a String and if so return that String. You do the typecheck correctly by using:
if(obj instanceof String) {
If obj is a String we would like to return this String, which means that we need to cast it to a String. You can therefore cast obj to a String and then save the obj String in the result variable:
result = (String) obj;
and then return result.
soo ao long tian
1,111 Pointsthis is my code, i wrote if(obj instanceof String) {....}, but it still fail.
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 = true;
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 String) {
return result;
}
}
}
Benjamin Barslev Nielsen
18,958 PointsLet's take an example of how the code should work. Let's say that obj = "Hello world". The function getTitleFromObject should then return the String "Hello World". We can however not just return obj, since it is of type Object. Therefore we make the instanceof check to see if "Hello World" is a String. "Hello World" is a String, so we need to tell Java that this is the case, and therefore we would like to use a type cast. This can be done by saying
(String) obj
This statement tells Java that obj is in fact a String, and therefore we can return the value of obj after we have told Java that it is a String. Below is how I have solved the challenge, and hopefully it makes sense, both how we can solve it this way and why we need the type cast.
String result = "";
if (obj instanceof String)
result = (String) obj;
return result;