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 trial

Java Java Data Structures Organizing Data Splitting Strings

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Can someone please help? i am stuck in here.. Thanks!

Can someone please help? I am stuck in here.. Thanks!

com/example/BlogPost.java
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

Hi 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
STAFF
Craig Dennis
Treehouse Teacher

Yeah, 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
Michael Smith
8,222 Points

Why don't we need brackets?

This solution worked for me... Everything is explained here, but if you still can't figure it out, then copy and paste this.:smile:

public String[] getWords() { 
 return mBody.split("\\s+"); 
} 

Hi 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
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

hello, is me again, i try this one and worked !!

public String[] getWords(){ return mBody.split("\s +");

}

i think it is nice,,,;)

Greg Austin
Greg Austin
10,890 Points

The toLowerCase() is unnecessary, but yeah, what he said lol those are the only two things I see wrong.

Ryan Publicover
Ryan Publicover
1,118 Points

public String getWords() { String words=BlogPost.split(" /s+"); return words; }

Is this close at all?

I 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
André Møller
913 Points

This should work.

public String [] getWords() { return mBody.split ("\s+");

}

André Møller
André Møller
913 Points

with 2 backslashes before the "S" !!

public String [] getWords() { return mBody.split ("\\s+");

}

Sander van Ockenburg-Zwaan
Sander van Ockenburg-Zwaan
17,726 Points

public String [] getWords() { return mBody.split ("\s+");

}

Even 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.

Zubeyr Aciksari
Zubeyr Aciksari
21,074 Points

Here 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.)