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

Juan Prieto
Juan Prieto
976 Points

Could you guide me without giving me the answer? What Am I doing wrong?

I don't know if I'm using "while" properly.

Tweet.java
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(){
        int count = 0;
      while( MAX_CHARS >= text.lenght()){
           count ++;
      return count;
    }

    }
}

2 Answers

Doli Harahap
Doli Harahap
6,246 Points

What are you trying to do here?

public int getRemainingCharacterCount(){ int count = 0; while( MAX_CHARS >= text.lenght()){ count ++; return count; } }

  1. Your return statement is inside the while, which of course will return 1.
  2. Your while condition is never ending looping.

What do you want to achieve?

Juan Prieto
Juan Prieto
976 Points

Ok so this is how twitter works. 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.

Thats what I basically have to do. Could guide me?

Doli Harahap
Doli Harahap
6,246 Points

ok. First, you text.length got typo.

second, why do you need to use while loop? is that a requirement? I thought you just want to get the remaining text length available, right?

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

or am I missing something? I paused my subscription, so I can't look at the challenge you do.