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

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Create a public method named getRemainingCharacterCount that returns an int representing how many characters they have l

I simple have no idea how to get past this code challenge can somebody please help I am still new to java and some of these explanations I find them confusing:

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

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

  public String getText() {
    return mText;
  }

  public getRemainingCharacterCount(){
    return int mText < 140;
  }

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

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

  public String getText() {
    return mText;
  }

  public getRemainingCharacterCount(){
    return int mText < 140;
  }

}

6 Answers

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

You don't need to create mtext. you shouldn't replace text to mtext, because of this you created an error. Hope this helps.

Thanks, Lawrence

Robert Stamate
Robert Stamate
13,227 Points

For me ``` 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(); } } ```

It worked out perfectly. I haven't changed this.text and it was easier.

Thanks Robert!

The mText stuff was driving me crazy with lots of compile errors. Replacing the 2nd part with {this.text = text;} saved me.

Kourosh Raeen
Kourosh Raeen
23,733 Points

You can subtract the length of mText from the maximum number of characters, MAX_CHARS, to find the remaining character count.

Christopher Mlalazi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points

Hi Kourosh, how do Ido that? Like this? But still this is not working. I now just want to get past this code challenge I will return back to it one day when I know more java to try to analyse it. :)

 public getRemainingCharacterCount(){
    return MAX_CHARS - mText.length;
  }
Kourosh Raeen
Kourosh Raeen
23,733 Points

You're almost there. The method needs a return type which should be int, so add that after public. Also you've forgotten the parenthesis for the length() method.

Kourosh Raeen
Kourosh Raeen
23,733 Points

The problem may actually be related to the constructor. You only need to set mText in there:

public Tweet(String text) {
    mText = text;
}
chase singhofen
chase singhofen
3,811 Points

depending on where you inserted it, you may need to delete or add some {} these always get me. ALAWAYS. good luck

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

i put it exactly like this, not messing with any of my braces. took me like 15min.

chase singhofen
chase singhofen
3,811 Points

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

worked for me, but i had lots of issues with my braces {}