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 - Retired Organizing Data Splitting Strings

Please help!! Frustrated and stuck!!!

i'm getting an error :did you add getWords methodl

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[] getWords(String body){

    return body.split("\\s+");
  }


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

2 Answers

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Tatenda,

the code challenge wants you to write a method that returns the words of the body of the blog post. Those words are stored in the field "mBody". You can access this field directly out of the getWords() method that you have written.

Your current implementation of the method is waiting for a String called "body" as a Parameter and will process this.

To give you a hint:

Remove the parameter "String body" from the method declaration and try to access the field "mBody" of the BlogPost class directly.

  public String[] getWords(){
    // Access the mBody field here instead of using a parameter.
  }
Greg Kaleka
Greg Kaleka
39,021 Points

Hi Tatenda,

That's a crappy error message. The problem is that your method doesn't need to take any parameters. Since this is a method on a class, and we already have the body in the property mBody, you can simply write:

method.java
  public String[] getWords(){

    return mBody.split("\\s+");
  }

We want the method to return an array of the post's own body's words. Not be able to parse any string at all and return an array. Does that make sense?

Happy coding! :thumbsup:

-Greg

Programming is one of those things that makes you want to put a bullet in your computer. hahaha but you have to hang in there. thanks guys.. greatly appreciated