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 trialZubeyr Aciksari
21,074 PointsCan someone please help? i am stuck in here.. Thanks!
Can someone please help? I am stuck in here.. Thanks!
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;
}
public String[] getWords() {
return mDescription.toLowerCase().split("[\\s+]");
}
}
11 Answers
Robert Richey
Courses Plus Student 16,352 PointsHi Zubeyr,
We need to return words from the body of the blog post. There is no member variable called mDescription
. What variable can we use that contains the body?
Also, for the regex pattern, when using brackets []
, the plus sign +
needs to go outside when trying to match one or more. When placed inside the brackets, the plus sign will be literally matched.
Hope this helps,
Cheers
Craig Dennis
Treehouse TeacherYeah, you don't need the brackets on this one. That's the problem, I'll see if I can make that more clear.
Michael Smith
8,222 PointsWhy don't we need brackets?
nfs
35,526 PointsThis solution worked for me... Everything is explained here, but if you still can't figure it out, then copy and paste this.
public String[] getWords() {
return mBody.split("\\s+");
}
Ararat Danielyan
Full Stack JavaScript Techdegree Student 4,173 PointsHi Nafis. I had the same problem. Thank you for your help. But I didn't understand why did you write String[]. Why do we need those brackets? Thank you,
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points} public String getWords(){ return mBody.split("\s +");
}
can be like this?
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Pointshello, is me again, i try this one and worked !!
public String[] getWords(){ return mBody.split("\s +");
}
i think it is nice,,,;)
Greg Austin
10,890 PointsThe toLowerCase() is unnecessary, but yeah, what he said lol those are the only two things I see wrong.
Ryan Publicover
1,118 Pointspublic String getWords() { String words=BlogPost.split(" /s+"); return words; }
Is this close at all?
Will Anderson
28,683 PointsI tried this: public String[] getWords() { return mBody.split("[\s +]"); } and was greeted with: Bummer! Expected 11 words, but received 12.
Awesome, huh? So I moved the '+' char out of the brackets, and the challenge passed! public String[] getWords() { return mBody.split("[\s ]+"); } Kudos to Robert Richey for that advice.
LOL Awesomer!
André Møller
913 PointsThis should work.
public String [] getWords() { return mBody.split ("\s+");
}
André Møller
913 Pointswith 2 backslashes before the "S" !!
lin huang
4,284 Pointspublic String [] getWords() { return mBody.split ("\\s+");
}
Sander van Ockenburg-Zwaan
17,726 Pointspublic String [] getWords() { return mBody.split ("\s+");
}
Will Anderson
28,683 PointsEven though the bracket syntax is 'optional', I think it's easier to understand, and easier then to maintain. Try: public String[] getWords() { return mBody.split("[\s ]+"); }
Using traditional regex notation leaves less to wonder.
Craig Dennis
Treehouse TeacherDon't forget to escape the slash \\s
Iheke Iheke-agu
Full Stack JavaScript Techdegree Student 2,307 Pointsyou have to escape twice \
Zubeyr Aciksari
21,074 PointsHere is the question: Add a new method named getWords that returns the words from the body of the blog post. Since we don't need to worry about special characters, let's just use the regular expression pattern \s+ (or any one or more white space character) for the parameter to the split method. (Remember to escape the backslash in your Java code.)