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

Bummer! Expected 11 words, but received 3.

What did I do wrong? I felt I have done correctly.. What do they means 11 words I cant see that in compiler :S

or do I have to put a variable inside the method? ex. public String[] getword( String x){ ..}

or am I calling wrong variable mBody?

also question nr2. Why couldnt I call \s? I have to type \s+ instead :S really wired is it because we are using JAVA 8 or? Really confusing and I still dont understand what \w means, does that mean that it takes characters/strings/text

help please

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 mBody.split("[^\\w\\s+#@]+");}
}

2 Answers

Kevin Frostad
Kevin Frostad
3,483 Points

Your getWords method expects a List of strings. You need to add the splitted objects to a list before returning. try this:

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

Well I did it long ago ^_^ but .. still I dont understand why. What the heck is \s+ I dont think I understood the quiz question either..

Kevin Frostad
Kevin Frostad
3,483 Points

I know <i>\s</i> targets spaces in a string, as I've understood... The quiz question was based on making a <i>getter</i> which gets a list og each word in the <i>body</i>. Then if you later wanted to get specific words from the <i>body</i>, you could just <i>iterate</i> that list, by using the <i>getWords()</i> method (called a getter). The pluss sign I don't know.

You could of course have iterated a class field of type list. But the you wouldn't be able to access it from other classes, assuming it would be private for security resons.

plus means .. if you have more than elements in the list .. I guess :)

btw thanks I understand!