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 trialKenneth Balajadia
2,009 PointsThe method getTitleFromObject will be called and passed a String and/or a com.example.BlogPost. For this first task, ret
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.
i dont know how to figure it out the hint 2. after i click recheck work it says
Bummer! Hint 2: you can cast an object to a string like this (String) obj
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 = true;
public static String getTitleFromObject(Object obj) {
// Fix this result variable to be the correct string.
if (obj instanceof String) {
return (String) obj;
} else {
return ((BlogPost) obj).getTitle();
}
}
}
4 Answers
Grigorij Schleifer
10,365 PointsHy Kenneth,
I have fixed it
public static String getTitleFromObject(Object obj) {
String result = ""; // use the String to store the Title from BlogPost
if (obj instanceof String) {
return (String) obj;
} else {
result = ((BlogPost) obj).getTitle(); // store the Title from BlogPost into result
}
return result; // return Title if obj is a BlogPost
}
Makes sense?
Grigorij
Alberto Castro
9,311 PointsI have just one question what is the reason for the parenthesis I'm pointing at here
--->((BlogPost) obj)<---.getTitle()
Why is not
(BlogPost) obj.getTitle()
Kentessa Fanfair
11,261 PointsThe reason for the parenthesis is to ensure that the object will be typecast as BlogPost before the getTitle() method is called. Since the getTitle() method belongs to the BlogPost class if you chain that method to an object before it is typecast the compiler might throw an error because the object is not of type BlogPost therefore getTitle() method does not exist on said object.
charles holder
1,944 Pointsso slightly confused on... get title takes a String and returns and String so how is it used on the obj that is being cast to BlogPost if BlogPost Is not a String? thanks in advane.
Fernando Martinez
Android Development Techdegree Student 921 Pointshello.! guys. I'm rookie. how did you know the answer of that question? I couldn't figure out even the first part of the answer. the only part that I can figure out was the "if()" and was because I did a search.
can you explain me how to figure out an answer for those type of question? thx.
Norbert Weinkauf
Courses Plus Student 2,293 PointsThe first task didn't work for me either. I've had had to give the right answer of the second task for the first. It seems to a bug. I guess the right answer to the first quesstion would just be:
return (String) obj;