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 Creating the MVP Remaining Characters

getRemainingCharacterCount

I need help i've written my code like that but i keep getting an error message so i can't seem to find the error i'm making.

Tweet.java
public class Tweet {
  private String text;
  public static final int MAX_CHARS = 140;

  public Tweet(String text) {
    while(text.length() < MAX_CHARS) {
     this.text = text; 
    }
   }

  public String getText() {
    return text;
  }

  public int getRemainingCharacterCount() {
   return MAX_CHARS - text.length(); 
  }

  public void setText(String text) {
    this.text = text;
  }
}
Jeroen Oosterwijk
Jeroen Oosterwijk
5,456 Points

Hey Elias Svoba

When I pass in the exact code you passed in, I too get an error without explanation, it seems to me there is some bug that causes the program that checks your solution to time out. Even though I'm not a pro, it seems to me your code is correct. You might consider reporting this bug to TeamTreehouse by shooting them an e-mail at help@teamtreehouse.com.

Here is a solution that does work:

    public class Tweet {
    private String text; 
    public static final int MAX_CHARS= 140;

    public Tweet(String text) { 
        this.text = text; }

    public String getText() { 
        return text; }

    public void setText(String text) { 
        this.text = text; }

    public int getRemainingCharacterCount(){ 
        return MAX_CHARS - text.length(); } }

Phew your solution works wonders thanks a lot Jeroen Oosterwijk. I've also reported it to TeamTreehouse. Happy coding.

1 Answer

public Tweet(String text) {
    while(text.length() < MAX_CHARS) {
     this.text = text; 

Hi Elias Svoba I assume the purpose of the code is to check if the tweet is longer than 140 characters. You should use “if” instead of "while". Every time you input text shorter the 140 characters you enter an infinite loop.

Thanks Ruslan Georgiev.