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 Objects (Retired) Creating the MVP Remaining Characters

Napatchol Thaipanich
PLUS
Napatchol Thaipanich
Courses Plus Student 13,534 Points

help

I don't know why

Tweet.java
public class Tweet {
  private String mText;
  public static final int MAX_LENGTH = 140;

  public Tweet(String text) {
   while(text.length() < MAX_LENGTH){
    mText = text;
    }
  }

  public String getText() {
    return mText;
  }

  public int getRemainingCharacters() {
    int remain =MAX_LENGTH-mText.length();
    return remain;
  }

}

A more descriptive question would help. Don't know why what?

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Napatchol,

your code is fine, but scip the while loop inside the constructor declaration.

public class Tweet {
  private String mText;
  public static final int MAX_LENGTH = 140;

  public Tweet(String text) {
    mText = text;
// you don´t need a while loop here
// when you create a new Tweet object the parameter text will be assigned to mText
// without using a loop
  }

  public String getText() {
    return mText;
  }

  public int getRemainingCharacters() {
    int remain =MAX_LENGTH-mText.length();
    return remain;
  }
}

You can design the getRemainingCharacters method a little bit clearer like this:

public int getRemainingCharacters() {
    return MAX_LENGTH- mText.length();
// the MAX_LENGTH- mText.length() statement will give you an integer
// and integer is a proper return type
// no need to declare a variable, assign a value to it and then return it
// just return the damn thing :)))))
}

Don´t hesitate to ask if something isn´t clear ...

Grigorij