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

Furqan Mujahid
Furqan Mujahid
3,367 Points

Add a new method named getWords that returns the words from the body of the blog post. Since we don't need to worry abou

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

This is the question

i am doing everything accordingly but still cant get it to pass.

Can someone help?

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;
  }
}

3 Answers

Joseph Wasden
Joseph Wasden
20,406 Points

I don't see a method by the name of getWords() in your linked code. That would be a good starting place.

From there, think about how to use regular expressions on the body content. Good luck!

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The problem is in your RegEx. The escape character needs to be escaped as well so it should look like this: ("\\s+"). Hope this helps! :sparkles:

Furqan Mujahid
Furqan Mujahid
3,367 Points

Its still no working. Thanks for the help tho.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

That's truly odd because if I copy/paste your code into the challenge and fix the spacing etc, then change the regex to what I posted... it passes.

Here's your code reformatted with the correct RegEx. You might need to reload the challenge:

  public String[] getWords() {
    //new method named getWords 
    String words[] = mBody.split("\\s+"); //create array, split mBody on white space and puts words in array 
    return words; //return array
  }
Furqan Mujahid
Furqan Mujahid
3,367 Points

You have to create it yourself: public String[] getWords() { //new method named getWords String words[] = mBody.split("\s+"); //create array, split mBody on white space and puts words in array return words; //return array }

but still not accepting so i need help