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

Damjan Vlaic
Damjan Vlaic
19,244 Points

---JAVA---

pls help someone :) This is my problem: Let's let them know how many characters they have left to use...so they can jam them full of hashtags #jklol

Create a public method named getRemainingCharacterCount that returns an int representing how many characters they have left before they 140. Base your calculation on the field that stores the current text.

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

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

  public String getText() {
    return text;
  }

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

  public int getRemainingCharacterCount() {
    //i tried this but i know it is not good --> return MAX_CHARS - text.lenght();  
  }
}
GhostIn Shell
GhostIn Shell
1,967 Points

Why isn't return MAX_CHARS - text.length(); working???

2 Answers

GhostIn Shell
GhostIn Shell
1,967 Points

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

this worked for me... I added text = ""; to the first method. Also, check the spelling of length. That was getting me for awhile.

Samuel Ferree
Samuel Ferree
31,722 Points

I mean, that's how I would do it. I would just add a null check tho

public int getRemainingCharacterCount() {
    return MAX_CHARS - (text != null ? text.length() : 0);  
}
Damjan Vlaic
Damjan Vlaic
19,244 Points

thank you!!! it works ;)