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 trialAbdelrahman Eltahir
Courses Plus Student 2,314 Pointscompiler tells gives me an error of "it looks like task 1 is no longer passing" . and i don't get it
can someone please tell me where i've went wrong
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(Object obj) {
// Fix this result variable to be the correct string.
if (obj instanceof Object) {
BlogPost result = (BlogPost) obj;
return result.getTitle();
} else{
return "";
}
}
}
2 Answers
Kevin Faust
15,353 PointsHi there,
Well for the first challenge you want to check if it is an instance of a String.
obj instanceof Object
That line is useless as our obj is already an object. In the paramater, we pass in an Object right? So whatever we pass in, whether it be a string or something else, it will be automatically upcasted to an Object. Since Object is really vague as everything is an object, we want to check if the thing we passed in is a string. So it would look like this:
if (obj instanceof String) {
return (String) obj;
}
For the second challenge, you got this piece of code:
if(obj instanceof Object){
BlogPost result = (BlogPost) obj;
return result.getTitle();
}
You want to change that Object to BlogPost. So if we indeed passed in a BlogPost object, we can downcast it appropriately and get the title from it.
I think you should know know how to solve it from here. If you still need help, let me know !
Happy coding and best of luck,
Kevin
Abdelrahman Eltahir
Courses Plus Student 2,314 PointsYes kevin your Assumption was correct , i used to delete the answer of task 1 everytime i passed it which was very silly of me , don't know why i did that , Thanks for your help i really appreciate it.
Kevin Faust
15,353 PointsWell glad we got it working and glad to help
Abdelrahman Eltahir
Courses Plus Student 2,314 PointsAbdelrahman Eltahir
Courses Plus Student 2,314 PointsKevin Faust
15,353 PointsKevin Faust
15,353 PointsWell obviously you will get that error as where did part 1 of the challenge go? Did you delete it? It works completely fine in terms of the second part of the challenge...
Im assuming you passed task 1 but then you deleted it for task 2. You want to keep both